-
Notifications
You must be signed in to change notification settings - Fork 1
/
backboneDo-0.5.min.js
5 lines (4 loc) · 1.89 KB
/
backboneDo-0.5.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 a=this,b=a.Backbone;b.Do={};var c=a._,d=a.jQuery||a.Zepto,e=d.noop;b.Do.Message=b.Model.extend({url:e,fetch:e,save:e,destroy:e});var f=b.Do.Message.extend({});b.Do.Req=b.CQRS.Message.extend({emit:function(){b.Do.hub.emit(b.Do.hub.reqChannel,this.parse(this.toJSON()))},parse:function(a){return a},observe:function(a){b.Do.responseHandler.observe(this.id,a)}});var g=b.CQRS.hub={reqChannel:"do",defaults:{reqChannel:"do",resChannel:"done",responseId:"reqId"},init:function(a){var b=this;this.initialized||(this.initialized=!0,a=c.extend(this.defaults,a),a.getRequestId&&(this.getRequestId=a.getRequestId),a.parseResponse&&(this.parseResponse=a.parseResponse),this.reqChannel=a.reqChannel,this.on(a.resChannel,function(c){var d=new Response;d.set(this.parseResponse(c));var e=d.toJSON();d.reqId=b.getRequestId(e,a.responseId),this.emit("resolveResponse",d)}))},parseResponse:function(a){var b=a;return typeof b=="string"&&(b=JSON.parse(b)),b},getRequestId:function(a,b){return h(a,b)}};c.extend(g,b.Events),g.on=g.bind,g.emit=g.trigger,b.Do.ResponseHandler=function(){this.initialize.apply(this,arguments)},c.extend(b.Do.ResponseHandler.prototype,b.Events,{initialize:function(){this.observedRequests=[],b.Do.hub.on("resolveResponse",function(a){this.handle(a)},this)},handle:function(a){var b=this.getPendingResponse(a);b&&(b.callback(a),this.removePendingResponse(a))},observe:function(a,b){this.observedRequests.push({id:a,callback:b})},getPendingRequest:function(a){return c.detect(this.observedRequests,function(b){return b.id==a.reqId})},removePendingCommand:function(a){var b=c.indexOf(this.observedRequests,a);this.observedRequests.splice(b,1)}}),b.Do.responseHandler=new ResponseHandler;var h=function(a,b){var c=b.split("."),d=0,e=a;while(c[d])e=e&&e[c[d]],d++;return e}}).call(this)