mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 21:22:48 +01:00
Implemented historical commands
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
(function(){
|
||||
var commandr = {
|
||||
|
||||
};
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
commandr.Command = Command;
|
||||
|
||||
window.commandr = commandr;
|
||||
})();
|
||||
52
components/commandsFeature.js
Normal file
52
components/commandsFeature.js
Normal file
@@ -0,0 +1,52 @@
|
||||
(function(app){
|
||||
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);
|
||||
@@ -1,4 +1,6 @@
|
||||
(function(should){
|
||||
// Problems: no check for the circular references
|
||||
|
||||
(function(){
|
||||
function Container(store) {
|
||||
this.store = {};
|
||||
}
|
||||
@@ -12,7 +14,6 @@
|
||||
return reg;
|
||||
};
|
||||
|
||||
// TODO: Check for circular - dependencies
|
||||
Container.prototype.resolve = function(name) {
|
||||
var reg = this.store[name];
|
||||
if(reg == null) {
|
||||
@@ -25,18 +26,21 @@
|
||||
reg.resolved = inst;
|
||||
}
|
||||
|
||||
console.log(name + ' resolved', reg.resolved);
|
||||
return reg.resolved;
|
||||
};
|
||||
|
||||
Container.prototype.resolveProperties = function (instance) {
|
||||
for(var property in instance) {
|
||||
if(property[0] == '$' && instance[property] == null) {
|
||||
var name = property.substr(1, property.length - 1);
|
||||
instance[property] = this.resolve(name);
|
||||
for(var prop in instance) {
|
||||
if(!instance.hasOwnProperty(prop)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(instance[property] == null) {
|
||||
console.log('"' + property + '" property couldn\'t be resolved')
|
||||
if(prop[0] == '$' && instance[prop] == null) {
|
||||
var key = prop.substr(1, prop.length - 1);
|
||||
instance[prop] = this.resolve(key);
|
||||
|
||||
if(instance[prop] == null) {
|
||||
console.log('"' + prop + '" property couldn\'t be resolved')
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user