-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackboneCQRS-0.5.6.min.js
5 lines (4 loc) · 4.59 KB
/
backboneCQRS-0.5.6.min.js
1
2
3
4
5
// Backbone.CQRS.js
// (c) 2012 Jan Mühlemann
// Backbone.CQRS may be freely distributed under the MIT license.
(function(){var e=this,t=e.Backbone;t.CQRS={};var n=e._,r=e.jQuery||e.Zepto,i=r.noop;t.CQRS.Message=t.Model.extend({url:i,fetch:i,save:i,destroy:i});var s=t.CQRS.Message.extend({});t.CQRS.Command=t.CQRS.Message.extend({emit:function(e){e&&this.observe(e),t.CQRS.hub.emit(t.CQRS.hub.commandsChannel,this.parse(this.toJSON()))},parse:function(e){return e},observe:function(e){t.CQRS.eventHandler.observe(this.id,e)}});var o=t.CQRS.hub={commandsChannel:"commands",defaults:{commandsChannel:"commands",eventsChannel:"events",eventNameAttr:"name",eventModelIdAttr:"payload.id",eventResponseToCommandId:"commandId"},init:function(e){var t=this;this.initialized||(this.initialized=!0,e=n.extend(this.defaults,e),e.parseEvent&&(this.parseEvent=e.parseEvent),e.getCommandId&&(this.getCommandId=e.getCommandId),this.commandsChannel=e.commandsChannel,this.on(e.eventsChannel,function(n){var r=new s;r.set(this.parseEvent(n));var i=r.toJSON();r.name=f(i,e.eventNameAttr),r.id=f(i,e.eventModelIdAttr),r.cmdId=t.getCommandId(i,e.eventResponseToCommandId),this.emit("dispatchEvent",r)}))},parseEvent:function(e){var t=e;return typeof t=="string"&&(t=JSON.parse(t)),t},getCommandId:function(e,t){return f(e,t)}};n.extend(o,t.Events),o.on=o.bind,o.emit=o.trigger,t.CQRS.EventDenormalizer=function(e){e=e||{},e.forEvent&&(this.forEvent=e.forEvent),e.forModel&&(this.forModel=e.forModel),e.payloadValue&&(this.payloadValue=e.payloadValue),e.modelIdAttr&&(this.modelIdAttr=e.modelIdAttr),e.onHandle&&(this.onHandle=e.onHandle),this.methode=e.methode||"update",this.model=e.model,this.collection=e.collection,this.forEvent&&this.forModel&&this.register.apply(this),this.initialize.apply(this,arguments)},n.extend(t.CQRS.EventDenormalizer.prototype,t.Events,{defaultPayloadValue:"payload",initialize:i,handle:function(e){var t=this.modelIdAttr?f(e.toJSON(),this.modelIdAttr):e.id;if(this.methode!=="create")t&&this.trigger("change:"+t,this.parse(e),this.apply(this.methode));else{var n=typeof this.collection=="function"?this.collection(e):this.collection;if(!n)return;var r=this.parse(e);n.add(r),this.onHandle&&this.onHandle(r,n.get(t))}},apply:function(e){var t=this;return function(n,r){e==="delete"?(r.isCQRSBound&&r.unbindCQRS(),r.destroy(),t.onHandle&&t.onHandle(n,r)):(r.set(n),t.onHandle&&t.onHandle(n,r))}},parse:function(e){return this.payloadValue||this.defaultPayloadValue?(diveTo=this.payloadValue||this.defaultPayloadValue,f(e.toJSON(),diveTo)):e.toJSON()},register:function(e,n){this.forEvent=e||this.forEvent,this.forModel=n||this.forModel,t.CQRS.eventHandler.register(this)}}),t.CQRS.EventDenormalizer.extend=t.Model.extend;var u=t.CQRS.EventDenormalizer.extend({initialize:function(){this.denormalizers=[],this.observedCommands=[],t.CQRS.hub.on("dispatchEvent",function(e){this.handle(e)},this)},getDenormalizer:function(e,t){return e?n(this.denormalizers).filter(function(t){return t.forEvent==e}):t?n(this.denormalizers).filter(function(e){return e.forModel==t}):null},handle:function(e){var t=this.getDenormalizer(e.name);n(t).each(function(t){t.handle(e)});var r=this.getPendingCommand(e);r&&(r.callback(e),this.removePendingCommand(r))},bind:function(e,t,r){if(e.indexOf(":")<0)return!1;var i=e.substring(0,e.indexOf(":")),s="change:"+e.substring(e.indexOf(":")+1,e.length),o=this.getDenormalizer(null,i);n(o).each(function(e){e.bind(s,t,r)})},unbind:function(e,t){if(e.indexOf(":")<0)return!1;var r=e.split(":"),i=r[0],s="change:"+r[1],o=this.getDenormalizer(null,i);n(o).each(function(e){e.unbind(s,t)})},observe:function(e,t){this.observedCommands.push({id:e,callback:t})},getPendingCommand:function(e){return n.detect(this.observedCommands,function(t){return t.id==e.cmdId})},removePendingCommand:function(e){var t=n.indexOf(this.observedCommands,e);this.observedCommands.splice(t,1)},register:function(e){this.denormalizers.push(e)}});t.CQRS.eventHandler=new u,t.Model=t.Model.extend({modelName:null,bindCQRS:function(e){e&&(this.modelName=e);if(!this.modelName)return;var n=this.id||this.cid;t.CQRS.eventHandler.bind(this.modelName+":"+n,this.apply,this),this.isCQRSBound=!0},unbindCQRS:function(e){e&&(this.modelName=e);if(!this.modelName)return;var n=this.id||this.cid;t.CQRS.eventHandler.unbind(this.modelName+":"+n,this.apply,this),this.isCQRSBound=!1},apply:function(e,t){t.apply(this,[e,this])}});var a=t.sync;t.CQRS.sync=function(e,t,n){var r=l[e];if(r!=="GET")return n.success(t,null,n);a(e,t,n)};var f=function(e,t){var n=t.split("."),r=0,i=e;while(n[r])i=i&&i[n[r]],r++;return i},l={create:"POST",update:"PUT","delete":"DELETE",read:"GET"}}).call(this)