diff --git a/dist/dijon.js b/dist/dijon.js index 03061bf..fc63626 100644 --- a/dist/dijon.js +++ b/dist/dijon.js @@ -587,7 +587,29 @@ } return this; - } + }, + + mapCommand: function ( eventName, commandClass, executionFunc, passEvent ) + { + var key = eventName + "_command"; + var handler = executionFunc || "execute"; + + this.mapClass( key, commandClass ); + this.mapHandler( eventName, key, handler, false, passEvent ); + + return this; + }, + + unmapCommand: function ( eventName, commandClass, executionFunc ) + { + var key = eventName + "_command"; + var handler = executionFunc || "execute"; + + this.unmap( key ); + this.unmapHandler( eventName, key, handler ); + + return this; + } };//dijon.System.prototype diff --git a/dist/dijon.min.js b/dist/dijon.min.js index 7d8a4b5..a6ff631 100644 --- a/dist/dijon.min.js +++ b/dist/dijon.min.js @@ -1 +1 @@ -/*! dijon - v0.6.1 - 2013-09-03 Copyright (c) 2013 Camille Reynders (http://www.creynders.be/); Licensed MIT */(function(t,e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):t.dijon=e()})(this,function(){"use strict";var t="no mapping found for this key",e={version:"0.6.1"};return e.System=function(){this._mappings={},this._outlets={},this._handlers={},this.strictInjections=!0,this.autoMapOutlets=!1,this.postInjectionHook="setup"},e.System.prototype={_createAndSetupInstance:function(t,e){var n=new e;return this.injectInto(n,t),n},_retrieveFromCacheOrCreate:function(e,n){n===void 0&&(n=!1);var s;if(!this._mappings.hasOwnProperty(e))throw t;var i=this._mappings[e];return!n&&i.isSingleton?(null==i.object&&(i.object=this._createAndSetupInstance(e,i.clazz)),s=i.object):s=i.clazz?this._createAndSetupInstance(e,i.clazz):i.object,s},mapOutlet:function(t,e,n){return e=e||"global",n=n||t,this._outlets.hasOwnProperty(e)||(this._outlets[e]={}),this._outlets[e][n]=t,this},getObject:function(t){return this._retrieveFromCacheOrCreate(t)},mapValue:function(t,e){return this._mappings[t]={clazz:null,object:e,isSingleton:!0},this.autoMapOutlets&&this.mapOutlet(t),this.hasMapping(t)&&this.injectInto(e,t),this},hasMapping:function(t){return this._mappings.hasOwnProperty(t)},mapClass:function(t,e){return this._mappings[t]={clazz:e,object:null,isSingleton:!1},this.autoMapOutlets&&this.mapOutlet(t),this},mapSingleton:function(t,e){return this._mappings[t]={clazz:e,object:null,isSingleton:!0},this.autoMapOutlets&&this.mapOutlet(t),this},instantiate:function(t){return this._retrieveFromCacheOrCreate(t,!0)},injectInto:function(t,e){if("object"==typeof t){var n=[];this._outlets.hasOwnProperty("global")&&n.push(this._outlets.global),e!==void 0&&this._outlets.hasOwnProperty(e)&&n.push(this._outlets[e]);for(var s in n){var i=n[s];for(var r in i){var a=i[r];(!this.strictInjections||r in t)&&(t[r]=this.getObject(a))}}"setup"in t&&t.setup.call(t)}return this},unmap:function(t){return delete this._mappings[t],this},unmapOutlet:function(t,e){return delete this._outlets[t][e],this},mapHandler:function(t,e,n,s,i){return e=e||"global",n=n||t,s===void 0&&(s=!1),i===void 0&&(i=!1),this._handlers.hasOwnProperty(t)||(this._handlers[t]={}),this._handlers[t].hasOwnProperty(e)||(this._handlers[t][e]=[]),this._handlers[t][e].push({handler:n,oneShot:s,passEvent:i}),this},unmapHandler:function(t,e,n){if(e=e||"global",n=n||t,this._handlers.hasOwnProperty(t)&&this._handlers[t].hasOwnProperty(e)){var s=this._handlers[t][e];for(var i in s){var r=s[i];if(r.handler===n){s.splice(i,1);break}}}return this},notify:function(t){var e=Array.prototype.slice.call(arguments),n=e.slice(1);if(this._handlers.hasOwnProperty(t)){var s=this._handlers[t];for(var i in s){var r,a=s[i];"global"!==i&&(r=this.getObject(i));var o,h,l=[];for(o=0,h=a.length;h>o;o++){var u,p=a[o];u=r&&"string"==typeof p.handler?r[p.handler]:p.handler,p.oneShot&&l.unshift(o),p.passEvent?u.apply(r,e):u.apply(r,n)}for(o=0,h=l.length;h>o;o++)a.splice(l[o],1)}}return this}},e}); \ No newline at end of file +/*! dijon - v0.6.1 - 2013-12-04 Copyright (c) 2013 Camille Reynders (http://www.creynders.be/); Licensed MIT */!function(a,b){"object"==typeof exports?module.exports=b():"function"==typeof define&&define.amd?define(b):a.dijon=b()}(this,function(){"use strict";var a="no mapping found for this key",b={version:"0.6.1"};return b.System=function(){this._mappings={},this._outlets={},this._handlers={},this.strictInjections=!0,this.autoMapOutlets=!1,this.postInjectionHook="setup"},b.System.prototype={_createAndSetupInstance:function(a,b){var c=new b;return this.injectInto(c,a),c},_retrieveFromCacheOrCreate:function(b,c){"undefined"==typeof c&&(c=!1);var d;if(!this._mappings.hasOwnProperty(b))throw a;var e=this._mappings[b];return!c&&e.isSingleton?(null==e.object&&(e.object=this._createAndSetupInstance(b,e.clazz)),d=e.object):d=e.clazz?this._createAndSetupInstance(b,e.clazz):e.object,d},mapOutlet:function(a,b,c){return b=b||"global",c=c||a,this._outlets.hasOwnProperty(b)||(this._outlets[b]={}),this._outlets[b][c]=a,this},getObject:function(a){return this._retrieveFromCacheOrCreate(a)},mapValue:function(a,b){return this._mappings[a]={clazz:null,object:b,isSingleton:!0},this.autoMapOutlets&&this.mapOutlet(a),this.hasMapping(a)&&this.injectInto(b,a),this},hasMapping:function(a){return this._mappings.hasOwnProperty(a)},mapClass:function(a,b){return this._mappings[a]={clazz:b,object:null,isSingleton:!1},this.autoMapOutlets&&this.mapOutlet(a),this},mapSingleton:function(a,b){return this._mappings[a]={clazz:b,object:null,isSingleton:!0},this.autoMapOutlets&&this.mapOutlet(a),this},instantiate:function(a){return this._retrieveFromCacheOrCreate(a,!0)},injectInto:function(a,b){if("object"==typeof a){var c=[];this._outlets.hasOwnProperty("global")&&c.push(this._outlets.global),"undefined"!=typeof b&&this._outlets.hasOwnProperty(b)&&c.push(this._outlets[b]);for(var d in c){var e=c[d];for(var f in e){var g=e[f];(!this.strictInjections||f in a)&&(a[f]=this.getObject(g))}}"setup"in a&&a.setup.call(a)}return this},unmap:function(a){return delete this._mappings[a],this},unmapOutlet:function(a,b){return delete this._outlets[a][b],this},mapHandler:function(a,b,c,d,e){return b=b||"global",c=c||a,"undefined"==typeof d&&(d=!1),"undefined"==typeof e&&(e=!1),this._handlers.hasOwnProperty(a)||(this._handlers[a]={}),this._handlers[a].hasOwnProperty(b)||(this._handlers[a][b]=[]),this._handlers[a][b].push({handler:c,oneShot:d,passEvent:e}),this},unmapHandler:function(a,b,c){if(b=b||"global",c=c||a,this._handlers.hasOwnProperty(a)&&this._handlers[a].hasOwnProperty(b)){var d=this._handlers[a][b];for(var e in d){var f=d[e];if(f.handler===c){d.splice(e,1);break}}}return this},notify:function(a){var b=Array.prototype.slice.call(arguments),c=b.slice(1);if(this._handlers.hasOwnProperty(a)){var d=this._handlers[a];for(var e in d){var f,g=d[e];"global"!==e&&(f=this.getObject(e));var h,i,j=[];for(h=0,i=g.length;i>h;h++){var k,l=g[h];k=f&&"string"==typeof l.handler?f[l.handler]:l.handler,l.oneShot&&j.unshift(h),l.passEvent?k.apply(f,b):k.apply(f,c)}for(h=0,i=j.length;i>h;h++)g.splice(j[h],1)}}return this},mapCommand:function(a,b,c,d){var e=a+"_command",f=c||"execute";return this.mapClass(e,b),this.mapHandler(a,e,f,!1,d),this},unmapCommand:function(a,b,c){var d=a+"_command",e=c||"execute";return this.unmap(d),this.unmapHandler(a,d,e),this}},b}); \ No newline at end of file diff --git a/src/dijon.js b/src/dijon.js index 03061bf..fc63626 100644 --- a/src/dijon.js +++ b/src/dijon.js @@ -587,7 +587,29 @@ } return this; - } + }, + + mapCommand: function ( eventName, commandClass, executionFunc, passEvent ) + { + var key = eventName + "_command"; + var handler = executionFunc || "execute"; + + this.mapClass( key, commandClass ); + this.mapHandler( eventName, key, handler, false, passEvent ); + + return this; + }, + + unmapCommand: function ( eventName, commandClass, executionFunc ) + { + var key = eventName + "_command"; + var handler = executionFunc || "execute"; + + this.unmap( key ); + this.unmapHandler( eventName, key, handler ); + + return this; + } };//dijon.System.prototype