2014-09-15 16:08:26 +00:00
|
|
|
const PayloadSources = {
|
2014-09-15 22:56:43 +00:00
|
|
|
VIEW_ACTION: "VIEW_ACTION",
|
|
|
|
SERVER_ACTION: "SERVER_ACTION"
|
2014-09-15 16:08:26 +00:00
|
|
|
};
|
|
|
|
|
2014-09-16 04:26:16 +00:00
|
|
|
|
2014-09-17 00:13:37 +00:00
|
|
|
function Dispatcher() {
|
2014-09-16 09:06:30 +00:00
|
|
|
this.callbacks = [];
|
|
|
|
}
|
2014-09-17 00:13:37 +00:00
|
|
|
Dispatcher.prototype.register = function (callback) {
|
2014-09-16 09:06:30 +00:00
|
|
|
this.callbacks.push(callback);
|
|
|
|
};
|
2014-09-17 00:13:37 +00:00
|
|
|
Dispatcher.prototype.unregister = function (callback) {
|
2014-09-16 09:06:30 +00:00
|
|
|
var index = this.callbacks.indexOf(f);
|
|
|
|
if (index >= 0) {
|
|
|
|
this.callbacks.splice(this.callbacks.indexOf(f), 1);
|
2014-09-15 22:56:43 +00:00
|
|
|
}
|
2014-09-16 09:06:30 +00:00
|
|
|
};
|
2014-09-17 00:13:37 +00:00
|
|
|
Dispatcher.prototype.dispatch = function (payload) {
|
2014-09-16 09:06:30 +00:00
|
|
|
console.debug("dispatch", payload);
|
2014-09-17 00:13:37 +00:00
|
|
|
this.callbacks.forEach(function (callback) {
|
2014-09-16 09:06:30 +00:00
|
|
|
callback(payload);
|
|
|
|
});
|
|
|
|
};
|
2014-09-16 04:26:16 +00:00
|
|
|
|
2014-09-15 16:08:26 +00:00
|
|
|
|
|
|
|
AppDispatcher = new Dispatcher();
|
2014-09-17 00:13:37 +00:00
|
|
|
AppDispatcher.dispatchViewAction = function (action) {
|
2014-09-15 22:56:43 +00:00
|
|
|
action.actionSource = PayloadSources.VIEW_ACTION;
|
|
|
|
this.dispatch(action);
|
2014-09-15 22:05:06 +00:00
|
|
|
};
|
2014-09-17 00:13:37 +00:00
|
|
|
AppDispatcher.dispatchServerAction = function (action) {
|
2014-09-15 22:56:43 +00:00
|
|
|
action.actionSource = PayloadSources.SERVER_ACTION;
|
|
|
|
this.dispatch(action);
|
2014-09-16 04:26:16 +00:00
|
|
|
};
|