From 1e6ba8096ade8e3dbc1ebf91d9a54a26f28eb68a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kawalec?= Date: Fri, 25 Sep 2015 12:49:00 +0200 Subject: [PATCH] multiple methods may be bound to the same action --- lib/componentMixin.js | 10 ++++++++-- lib/store.js | 22 +++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/componentMixin.js b/lib/componentMixin.js index fbed29f..6905d60 100644 --- a/lib/componentMixin.js +++ b/lib/componentMixin.js @@ -80,7 +80,11 @@ module.exports = { throw new Error('Components may only bind to before, failed and after events'); } - self._actionMapper[ key ] = cb; + if(!Array.isArray(self._actionMapper[ key ])) { + self._actionMapper[ key ] = []; + } + + self._actionMapper[ key ].push(cb); }); }, @@ -181,7 +185,9 @@ module.exports = { */ onDispatch : function onDispatch(payload) { if (this.isMounted() && this._actionMapper[ payload.actionType ]) { - this._actionMapper[ payload.actionType ](payload.actionType, payload.payload); + _.forEach(this._actionMapper[ payload.actionType ], function(action) { + action.call(this, payload.actionType, payload.payload); + }); } } }; diff --git a/lib/store.js b/lib/store.js index bffb724..0c108a3 100644 --- a/lib/store.js +++ b/lib/store.js @@ -50,8 +50,11 @@ BaseStore.prototype._initActions = function initActions(actions) { value.forEach(function processAction(name) { name = namespaceTransform(name); + if (!Array.isArray(self._actionTypes[name])) { + self._actionTypes[name] = []; + } - self._actionTypes[name] = method; + self._actionTypes[name].push(method); }); }); }; @@ -120,15 +123,16 @@ BaseStore.prototype.getStore = function getStore(name) { * @param {Object} payload */ BaseStore.prototype._processActionEvent = function _processActionEvent(payload) { - var method = this._actionTypes[ payload.actionType ]; - - if (method && this[ method ]) { - if (['replaceState', 'setState'].indexOf(method) === -1) { - this[ method ](payload.payload, payload.actionType); - } else { - this[ method ](payload.payload); + var methods = this._actionTypes[ payload.actionType ]; + _.forEach(methods, function checkMethod(method) { + if (method && this[ method ]) { + if (['replaceState', 'setState'].indexOf(method) === -1) { + this[ method ](payload.payload, payload.actionType); + } else { + this[ method ](payload.payload); + } } - } + }, this); return this; }; /*jshint ignore:line */