Implemented historical commands

This commit is contained in:
Borys Levytskyi
2015-04-03 19:31:57 +03:00
parent 8e5be22fe3
commit 022c65635c
12 changed files with 148 additions and 122 deletions

View File

@@ -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;
})();

View 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);

View File

@@ -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')
}
}
}