Files
BitwiseCmd/src.old/js_unused/commandsFeature.js
Borys_Levytskyi de0dfba04f React boilerplate
2016-11-20 18:59:57 +02:00

57 lines
1.4 KiB
JavaScript

(function(app, core){
"use strict";
var should = core.should;
function Command(name) {
this.name = name;
this.executionHandlers = [];
}
Command.prototype.execute = function (cmdArgs) {
cmdArgs = cmdArgs || {};
cmdArgs.commandHandled = false;
for(var i=0; i<this.executionHandlers.length; i++) {
this.executionHandlers[i](cmdArgs);
if(cmdArgs.commandHandled === true) {
return;
}
}
};
Command.prototype.subscribe = function (handler) {
this.executionHandlers.push(handler);
// TODO: unsubcribe
};
app.commandHandlers = {};
app.command = function(name, handler) {
var cmd = this.commandHandlers[name];
if(cmd == null) {
cmd = this.commandHandlers[name] = new commandr.Command(name);
}
if(typeof handler == "function") {
cmd.subscribe(handler);
}
if (typeof handler == "object") {
if(typeof handler.execute != "function"){
console.warn('Given handler is an object, but doesn\'t have "execute" function');
return cmd;
}
this.di.resolveProperties(handler);
cmd.subscribe(handler.execute.bind(handler));
}
return cmd;
};
window.commandr = commandr;
})(window.app, window.core);