backbone.wreqr.js

From disqus.com, 1 Week ago, written in JavaScript, viewed 3 times. This paste is a reply to backbone.marionette.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/754b1bff Embed
Download Paste or View Raw
  1. // Backbone.Wreqr (Backbone.Marionette)
  2. // ----------------------------------
  3. // v1.4.0
  4. //
  5. // Copyright (c)2016 Derick Bailey, Muted Solutions, LLC.
  6. // Distributed under MIT license
  7. //
  8. // http://github.com/marionettejs/backbone.wreqr
  9.  
  10.  
  11. (function(root, factory) {
  12.  
  13.   if (typeof define === 'function' && define.amd) {
  14.     define('backbone.wreqr',['backbone', 'underscore'], function(Backbone, _) {
  15.       return factory(Backbone, _);
  16.     });
  17.   } else if (typeof exports !== 'undefined') {
  18.     var Backbone = require('backbone');
  19.     var _ = require('underscore');
  20.     module.exports = factory(Backbone, _);
  21.   } else {
  22.     factory(root.Backbone, root._);
  23.   }
  24.  
  25. }(this, function(Backbone, _) {
  26.   "use strict";
  27.  
  28.   var previousWreqr = Backbone.Wreqr;
  29.  
  30.   var Wreqr = Backbone.Wreqr = {};
  31.  
  32.   Backbone.Wreqr.VERSION = '1.4.0';
  33.  
  34.   Backbone.Wreqr.noConflict = function () {
  35.     Backbone.Wreqr = previousWreqr;
  36.     return this;
  37.   };
  38.  
  39.   // Handlers
  40.   // --------
  41.   // A registry of functions to call, given a name
  42.  
  43.   Wreqr.Handlers = (function(Backbone, _){
  44.     "use strict";
  45.    
  46.     // Constructor
  47.     // -----------
  48.  
  49.     var Handlers = function(options){
  50.       this.options = options;
  51.       this._wreqrHandlers = {};
  52.      
  53.       if (_.isFunction(this.initialize)){
  54.         this.initialize(options);
  55.       }
  56.     };
  57.  
  58.     Handlers.extend = Backbone.Model.extend;
  59.  
  60.     // Instance Members
  61.     // ----------------
  62.  
  63.     _.extend(Handlers.prototype, Backbone.Events, {
  64.  
  65.       // Add multiple handlers using an object literal configuration
  66.       setHandlers: function(handlers){
  67.         _.each(handlers, _.bind(function(handler, name){
  68.           var context = null;
  69.  
  70.           if (_.isObject(handler) && !_.isFunction(handler)){
  71.             context = handler.context;
  72.             handler = handler.callback;
  73.           }
  74.  
  75.           this.setHandler(name, handler, context);
  76.         }, this));
  77.       },
  78.  
  79.       // Add a handler for the given name, with an
  80.       // optional context to run the handler within
  81.       setHandler: function(name, handler, context){
  82.         var config = {
  83.           callback: handler,
  84.           context: context
  85.         };
  86.  
  87.         this._wreqrHandlers[name] = config;
  88.  
  89.         this.trigger("handler:add", name, handler, context);
  90.       },
  91.  
  92.       // Determine whether or not a handler is registered
  93.       hasHandler: function(name){
  94.         return !! this._wreqrHandlers[name];
  95.       },
  96.  
  97.       // Get the currently registered handler for
  98.       // the specified name. Throws an exception if
  99.       // no handler is found.
  100.       getHandler: function(name){
  101.         var config = this._wreqrHandlers[name];
  102.  
  103.         if (!config){
  104.           return;
  105.         }
  106.  
  107.         return function(){
  108.           return config.callback.apply(config.context, arguments);
  109.         };
  110.       },
  111.  
  112.       // Remove a handler for the specified name
  113.       removeHandler: function(name){
  114.         delete this._wreqrHandlers[name];
  115.       },
  116.  
  117.       // Remove all handlers from this registry
  118.       removeAllHandlers: function(){
  119.         this._wreqrHandlers = {};
  120.       }
  121.     });
  122.  
  123.     return Handlers;
  124.   })(Backbone, _);
  125.  
  126.   // Wreqr.CommandStorage
  127.   // --------------------
  128.   //
  129.   // Store and retrieve commands for execution.
  130.   Wreqr.CommandStorage = (function(){
  131.     "use strict";
  132.  
  133.     // Constructor function
  134.     var CommandStorage = function(options){
  135.       this.options = options;
  136.       this._commands = {};
  137.  
  138.       if (_.isFunction(this.initialize)){
  139.         this.initialize(options);
  140.       }
  141.     };
  142.  
  143.     // Instance methods
  144.     _.extend(CommandStorage.prototype, Backbone.Events, {
  145.  
  146.       // Get an object literal by command name, that contains
  147.       // the `commandName` and the `instances` of all commands
  148.       // represented as an array of arguments to process
  149.       getCommands: function(commandName){
  150.         var commands = this._commands[commandName];
  151.  
  152.         // we don't have it, so add it
  153.         if (!commands){
  154.  
  155.           // build the configuration
  156.           commands = {
  157.             command: commandName,
  158.             instances: []
  159.           };
  160.  
  161.           // store it
  162.           this._commands[commandName] = commands;
  163.         }
  164.  
  165.         return commands;
  166.       },
  167.  
  168.       // Add a command by name, to the storage and store the
  169.       // args for the command
  170.       addCommand: function(commandName, args){
  171.         var command = this.getCommands(commandName);
  172.         command.instances.push(args);
  173.       },
  174.  
  175.       // Clear all commands for the given `commandName`
  176.       clearCommands: function(commandName){
  177.         var command = this.getCommands(commandName);
  178.         command.instances = [];
  179.       }
  180.     });
  181.  
  182.     return CommandStorage;
  183.   })();
  184.  
  185.   // Wreqr.Commands
  186.   // --------------
  187.   //
  188.   // A simple command pattern implementation. Register a command
  189.   // handler and execute it.
  190.   Wreqr.Commands = (function(Wreqr, _){
  191.     "use strict";
  192.  
  193.     return Wreqr.Handlers.extend({
  194.       // default storage type
  195.       storageType: Wreqr.CommandStorage,
  196.  
  197.       constructor: function(options){
  198.         this.options = options || {};
  199.  
  200.         this._initializeStorage(this.options);
  201.         this.on("handler:add", this._executeCommands, this);
  202.  
  203.         Wreqr.Handlers.prototype.constructor.apply(this, arguments);
  204.       },
  205.  
  206.       // Execute a named command with the supplied args
  207.       execute: function(name){
  208.         name = arguments[0];
  209.         var args = _.rest(arguments);
  210.  
  211.         if (this.hasHandler(name)){
  212.           this.getHandler(name).apply(this, args);
  213.         } else {
  214.           this.storage.addCommand(name, args);
  215.         }
  216.  
  217.       },
  218.  
  219.       // Internal method to handle bulk execution of stored commands
  220.       _executeCommands: function(name, handler, context){
  221.         var command = this.storage.getCommands(name);
  222.  
  223.         // loop through and execute all the stored command instances
  224.         _.each(command.instances, function(args){
  225.           handler.apply(context, args);
  226.         });
  227.  
  228.         this.storage.clearCommands(name);
  229.       },
  230.  
  231.       // Internal method to initialize storage either from the type's
  232.       // `storageType` or the instance `options.storageType`.
  233.       _initializeStorage: function(options){
  234.         var storage;
  235.  
  236.         var StorageType = options.storageType || this.storageType;
  237.         if (_.isFunction(StorageType)){
  238.           storage = new StorageType();
  239.         } else {
  240.           storage = StorageType;
  241.         }
  242.  
  243.         this.storage = storage;
  244.       }
  245.     });
  246.  
  247.   })(Wreqr, _);
  248.  
  249.   // Wreqr.RequestResponse
  250.   // ---------------------
  251.   //
  252.   // A simple request/response implementation. Register a
  253.   // request handler, and return a response from it
  254.   Wreqr.RequestResponse = (function(Wreqr, _){
  255.     "use strict";
  256.  
  257.     return Wreqr.Handlers.extend({
  258.       request: function(name){
  259.         if (this.hasHandler(name)) {
  260.           return this.getHandler(name).apply(this, _.rest(arguments));
  261.         }
  262.       }
  263.     });
  264.  
  265.   })(Wreqr, _);
  266.  
  267.   // Event Aggregator
  268.   // ----------------
  269.   // A pub-sub object that can be used to decouple various parts
  270.   // of an application through event-driven architecture.
  271.  
  272.   Wreqr.EventAggregator = (function(Backbone, _){
  273.     "use strict";
  274.     var EA = function(){};
  275.  
  276.     // Copy the `extend` function used by Backbone's classes
  277.     EA.extend = Backbone.Model.extend;
  278.  
  279.     // Copy the basic Backbone.Events on to the event aggregator
  280.     _.extend(EA.prototype, Backbone.Events);
  281.  
  282.     return EA;
  283.   })(Backbone, _);
  284.  
  285.   // Wreqr.Channel
  286.   // --------------
  287.   //
  288.   // An object that wraps the three messaging systems:
  289.   // EventAggregator, RequestResponse, Commands
  290.   Wreqr.Channel = (function(Wreqr){
  291.     "use strict";
  292.  
  293.     var Channel = function(channelName) {
  294.       this.vent        = new Backbone.Wreqr.EventAggregator();
  295.       this.reqres      = new Backbone.Wreqr.RequestResponse();
  296.       this.commands    = new Backbone.Wreqr.Commands();
  297.       this.channelName = channelName;
  298.     };
  299.  
  300.     _.extend(Channel.prototype, {
  301.  
  302.       // Remove all handlers from the messaging systems of this channel
  303.       reset: function() {
  304.         this.vent.off();
  305.         this.vent.stopListening();
  306.         this.reqres.removeAllHandlers();
  307.         this.commands.removeAllHandlers();
  308.         return this;
  309.       },
  310.  
  311.       // Connect a hash of events; one for each messaging system
  312.       connectEvents: function(hash, context) {
  313.         this._connect('vent', hash, context);
  314.         return this;
  315.       },
  316.  
  317.       connectCommands: function(hash, context) {
  318.         this._connect('commands', hash, context);
  319.         return this;
  320.       },
  321.  
  322.       connectRequests: function(hash, context) {
  323.         this._connect('reqres', hash, context);
  324.         return this;
  325.       },
  326.  
  327.       // Attach the handlers to a given message system `type`
  328.       _connect: function(type, hash, context) {
  329.         if (!hash) {
  330.           return;
  331.         }
  332.  
  333.         context = context || this;
  334.         var method = (type === 'vent') ? 'on' : 'setHandler';
  335.  
  336.         _.each(hash, _.bind(function(fn, eventName) {
  337.           this[type][method](eventName, _.bind(fn, context));
  338.         }, this));
  339.       }
  340.     });
  341.  
  342.  
  343.     return Channel;
  344.   })(Wreqr);
  345.  
  346.   // Wreqr.Radio
  347.   // --------------
  348.   //
  349.   // An object that lets you communicate with many channels.
  350.   Wreqr.radio = (function(Wreqr, _){
  351.     "use strict";
  352.  
  353.     var Radio = function() {
  354.       this._channels = {};
  355.       this.vent = {};
  356.       this.commands = {};
  357.       this.reqres = {};
  358.       this._proxyMethods();
  359.     };
  360.  
  361.     _.extend(Radio.prototype, {
  362.  
  363.       channel: function(channelName) {
  364.         if (!channelName) {
  365.           throw new Error('Channel must receive a name');
  366.         }
  367.  
  368.         return this._getChannel( channelName );
  369.       },
  370.  
  371.       _getChannel: function(channelName) {
  372.         var channel = this._channels[channelName];
  373.  
  374.         if(!channel) {
  375.           channel = new Wreqr.Channel(channelName);
  376.           this._channels[channelName] = channel;
  377.         }
  378.  
  379.         return channel;
  380.       },
  381.  
  382.       _proxyMethods: function() {
  383.         _.each(['vent', 'commands', 'reqres'], _.bind(function(system) {
  384.           _.each( messageSystems[system], _.bind(function(method) {
  385.             this[system][method] = proxyMethod(this, system, method);
  386.           }, this));
  387.         }, this));
  388.       }
  389.     });
  390.  
  391.  
  392.     var messageSystems = {
  393.       vent: [
  394.         'on',
  395.         'off',
  396.         'trigger',
  397.         'once',
  398.         'stopListening',
  399.         'listenTo',
  400.         'listenToOnce'
  401.       ],
  402.  
  403.       commands: [
  404.         'execute',
  405.         'setHandler',
  406.         'setHandlers',
  407.         'removeHandler',
  408.         'removeAllHandlers'
  409.       ],
  410.  
  411.       reqres: [
  412.         'request',
  413.         'setHandler',
  414.         'setHandlers',
  415.         'removeHandler',
  416.         'removeAllHandlers'
  417.       ]
  418.     };
  419.  
  420.     var proxyMethod = function(radio, system, method) {
  421.       return function(channelName) {
  422.         var messageSystem = radio._getChannel(channelName)[system];
  423.  
  424.         return messageSystem[method].apply(messageSystem, _.rest(arguments));
  425.       };
  426.     };
  427.  
  428.     return new Radio();
  429.  
  430.   })(Wreqr, _);
  431.  
  432.  
  433.   return Backbone.Wreqr;
  434.  
  435. }));
  436.  
  437. // https://c.disquscdn.com/next/node_modules/backbone.wreqr/lib/backbone.wreqr.js

Replies to backbone.wreqr.js rss

Title Name Language When
collapse.js disqus.com javascript 1 Week ago.

Reply to "backbone.wreqr.js"

Here you can reply to the paste above