You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12 lines
4.3 KiB
JavaScript

// Backbone.Wreqr (Backbone.Marionette)
// ----------------------------------
// v1.3.2
//
// Copyright (c)2014 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
//
// http://github.com/marionettejs/backbone.wreqr
(function(n,e){if("function"==typeof define&&define.amd)define(["backbone","underscore"],function(n,t){return e(n,t)});else if("undefined"!=typeof exports){var t=require("backbone"),r=require("underscore");module.exports=e(t,r)}else e(n.Backbone,n._)})(this,function(n,e){"use strict";var t=n.Wreqr,r=n.Wreqr={};return n.Wreqr.VERSION="1.3.2",n.Wreqr.noConflict=function(){return n.Wreqr=t,this},r.Handlers=function(n,e){var t=function(n){this.options=n,this._wreqrHandlers={},e.isFunction(this.initialize)&&this.initialize(n)};return t.extend=n.Model.extend,e.extend(t.prototype,n.Events,{setHandlers:function(n){e.each(n,function(n,t){var r=null;e.isObject(n)&&!e.isFunction(n)&&(r=n.context,n=n.callback),this.setHandler(t,n,r)},this)},setHandler:function(n,e,t){var r={callback:e,context:t};this._wreqrHandlers[n]=r,this.trigger("handler:add",n,e,t)},hasHandler:function(n){return!!this._wreqrHandlers[n]},getHandler:function(n){var e=this._wreqrHandlers[n];if(e)return function(){return e.callback.apply(e.context,arguments)}},removeHandler:function(n){delete this._wreqrHandlers[n]},removeAllHandlers:function(){this._wreqrHandlers={}}}),t}(n,e),r.CommandStorage=function(){var t=function(n){this.options=n,this._commands={},e.isFunction(this.initialize)&&this.initialize(n)};return e.extend(t.prototype,n.Events,{getCommands:function(n){var e=this._commands[n];return e||(e={command:n,instances:[]},this._commands[n]=e),e},addCommand:function(n,e){var t=this.getCommands(n);t.instances.push(e)},clearCommands:function(n){var e=this.getCommands(n);e.instances=[]}}),t}(),r.Commands=function(n,e){return n.Handlers.extend({storageType:n.CommandStorage,constructor:function(e){this.options=e||{},this._initializeStorage(this.options),this.on("handler:add",this._executeCommands,this),n.Handlers.prototype.constructor.apply(this,arguments)},execute:function(n){n=arguments[0];var t=e.rest(arguments);this.hasHandler(n)?this.getHandler(n).apply(this,t):this.storage.addCommand(n,t)},_executeCommands:function(n,t,r){var s=this.storage.getCommands(n);e.each(s.instances,function(n){t.apply(r,n)}),this.storage.clearCommands(n)},_initializeStorage:function(n){var t,r=n.storageType||this.storageType;t=e.isFunction(r)?new r:r,this.storage=t}})}(r,e),r.RequestResponse=function(n,e){return n.Handlers.extend({request:function(n){return this.hasHandler(n)?this.getHandler(n).apply(this,e.rest(arguments)):void 0}})}(r,e),r.EventAggregator=function(n,e){var t=function(){};return t.extend=n.Model.extend,e.extend(t.prototype,n.Events),t}(n,e),r.Channel=function(){var t=function(e){this.vent=new n.Wreqr.EventAggregator,this.reqres=new n.Wreqr.RequestResponse,this.commands=new n.Wreqr.Commands,this.channelName=e};return e.extend(t.prototype,{reset:function(){return this.vent.off(),this.vent.stopListening(),this.reqres.removeAllHandlers(),this.commands.removeAllHandlers(),this},connectEvents:function(n,e){return this._connect("vent",n,e),this},connectCommands:function(n,e){return this._connect("commands",n,e),this},connectRequests:function(n,e){return this._connect("reqres",n,e),this},_connect:function(n,t,r){if(t){r=r||this;var s="vent"===n?"on":"setHandler";e.each(t,function(t,i){this[n][s](i,e.bind(t,r))},this)}}}),t}(r),r.radio=function(n,e){var t=function(){this._channels={},this.vent={},this.commands={},this.reqres={},this._proxyMethods()};e.extend(t.prototype,{channel:function(n){if(!n)throw Error("Channel must receive a name");return this._getChannel(n)},_getChannel:function(e){var t=this._channels[e];return t||(t=new n.Channel(e),this._channels[e]=t),t},_proxyMethods:function(){e.each(["vent","commands","reqres"],function(n){e.each(r[n],function(e){this[n][e]=s(this,n,e)},this)},this)}});var r={vent:["on","off","trigger","once","stopListening","listenTo","listenToOnce"],commands:["execute","setHandler","setHandlers","removeHandler","removeAllHandlers"],reqres:["request","setHandler","setHandlers","removeHandler","removeAllHandlers"]},s=function(n,t,r){return function(s){var i=n._getChannel(s)[t];return i[r].apply(i,e.rest(arguments))}};return new t}(r,e),n.Wreqr});
//@ sourceMappingURL=backbone.wreqr.map