Re: Re:Events.js

From disqus.com, 2 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to Re:editable.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/e01c6389 Embed
Download Paste or View Raw
  1. define('core/Events',['require','core/utils/collection/each','core/utils/object/has','core/utils/uniqueId'],function (require) {
  2.     'use strict';
  3.  
  4.     var each = require('core/utils/collection/each');
  5.     var has = require('core/utils/object/has');
  6.     var uniqueId = require('core/utils/uniqueId');
  7.  
  8.     // Returns a function that will be executed at most one time, no matter how
  9.     // often you call it. Useful for lazy initialization.
  10.     var once = function (func) {
  11.         var ran = false;
  12.         var memo;
  13.  
  14.         return function () {
  15.             if (ran) return memo;
  16.             ran = true;
  17.             memo = func.apply(this, arguments);
  18.             func = null;
  19.             return memo;
  20.         };
  21.     };
  22.     var keys = Object.keys || function (obj) {
  23.         if (obj !== Object(obj)) throw new TypeError('Invalid object');
  24.         var keys = [];
  25.         for (var key in obj) if (has(obj, key)) keys[keys.length] = key;
  26.         return keys;
  27.     };
  28.     var slice = [].slice;
  29.  
  30.     // Backbone.Events
  31.     // ---------------
  32.  
  33.     // A module that can be mixed in to *any object* in order to provide it with
  34.     // custom events. You may bind with `on` or remove with `off` callback
  35.     // functions to an event; `trigger`-ing an event fires all callbacks in
  36.     // succession.
  37.     //
  38.     // var object = {};
  39.     // _.extend(object, Backbone.Events);
  40.     // object.on('expand', function(){ alert('expanded'); });
  41.     // object.trigger('expand');
  42.     //
  43.     var Events = {
  44.  
  45.         // Bind an event to a `callback` function. Passing `"all"` will bind
  46.         // the callback to all events fired.
  47.         on: function (name, callback, context) {
  48.             if (!eventsApi(this, 'on', name, [callback, context]) || !callback) return this;
  49.             this._events = this._events || {};
  50.             var events = this._events[name] || (this._events[name] = []);
  51.             events.push({ callback: callback, context: context, ctx: context || this });
  52.             return this;
  53.         },
  54.  
  55.         // Bind an event to only be triggered a single time. After the first time
  56.         // the callback is invoked, it will be removed.
  57.         once: function (name, callback, context) {
  58.             if (!eventsApi(this, 'once', name, [callback, context]) || !callback) return this;
  59.             var self = this;
  60.             var onced = once(function () {
  61.                 self.off(name, onced);
  62.                 callback.apply(this, arguments);
  63.             });
  64.             onced._callback = callback;
  65.             return this.on(name, onced, context);
  66.         },
  67.  
  68.         // Remove one or many callbacks. If `context` is null, removes all
  69.         // callbacks with that function. If `callback` is null, removes all
  70.         // callbacks for the event. If `name` is null, removes all bound
  71.         // callbacks for all events.
  72.         off: function (name, callback, context) {
  73.             var retain, ev, events, names, i, nameLen, j, evtLen;
  74.             if (!this._events || !eventsApi(this, 'off', name, [callback, context])) return this;
  75.             if (!name && !callback && !context) {
  76.                 this._events = {};
  77.                 return this;
  78.             }
  79.  
  80.             names = name ? [name] : keys(this._events);
  81.             for (i = 0, nameLen = names.length; i < nameLen; i++) {
  82.                 name = names[i];
  83.                 if (events = this._events[name]) {  // eslint-disable-line no-cond-assign
  84.                     this._events[name] = retain = [];
  85.                     if (callback || context) {
  86.                         for (j = 0, evtLen = events.length; j < evtLen; j++) {
  87.                             ev = events[j];
  88.                             if (
  89.                                 context && context !== ev.context ||
  90.                                 callback && callback !== ev.callback && callback !== ev.callback._callback
  91.                             )
  92.                                 retain.push(ev);
  93.                         }
  94.                     }
  95.                     if (!retain.length) delete this._events[name];
  96.                 }
  97.             }
  98.  
  99.             return this;
  100.         },
  101.  
  102.         // Trigger one or many events, firing all bound callbacks. Callbacks are
  103.         // passed the same arguments as `trigger` is, apart from the event name
  104.         // (unless you're listening on `"all"`, which will cause your callback to
  105.         // receive the true name of the event as the first argument).
  106.         trigger: function (name) {
  107.             if (!this._events) return this;
  108.             var args = slice.call(arguments, 1);
  109.             if (!eventsApi(this, 'trigger', name, args)) return this;
  110.             var events = this._events[name];
  111.             var allEvents = this._events.all;
  112.             if (events) triggerEvents(events, args);
  113.             if (allEvents) triggerEvents(allEvents, arguments);
  114.             return this;
  115.         },
  116.  
  117.         // Tell this object to stop listening to either specific events ... or
  118.         // to every object it's currently listening to.
  119.         stopListening: function (obj, name, callback) {
  120.             var listeners = this._listeners;
  121.             if (!listeners) return this;
  122.             var deleteListener = !name && !callback;
  123.             if (typeof name === 'object') callback = this;
  124.             if (obj) (listeners = {})[obj._listenerId] = obj;
  125.             for (var id in listeners) {
  126.                 listeners[id].off(name, callback, this);
  127.                 if (deleteListener) delete this._listeners[id];
  128.             }
  129.             return this;
  130.         },
  131.  
  132.     };
  133.  
  134.     // Regular expression used to split event strings.
  135.     var eventSplitter = /\s+/;
  136.  
  137.     // Implement fancy features of the Events API such as multiple event
  138.     // names `"change blur"` and jQuery-style event maps `{change: action}`
  139.     // in terms of the existing API.
  140.     var eventsApi = function (obj, action, name, rest) {
  141.         if (!name) return true;
  142.  
  143.         // Handle event maps.
  144.         if (typeof name === 'object') {
  145.             for (var key in name)
  146.                 obj[action].apply(obj, [key, name[key]].concat(rest));
  147.  
  148.             return false;
  149.         }
  150.  
  151.         // Handle space separated event names.
  152.         if (eventSplitter.test(name)) {
  153.             var names = name.split(eventSplitter);
  154.             for (var i = 0, len = names.length; i < len; i++)
  155.                 obj[action].apply(obj, [names[i]].concat(rest));
  156.  
  157.             return false;
  158.         }
  159.  
  160.         return true;
  161.     };
  162.  
  163.     // A difficult-to-believe, but optimized internal dispatch function for
  164.     // triggering events. Tries to keep the usual cases speedy (most internal
  165.     // Backbone events have 3 arguments).
  166.     var triggerEvents = function (events, args) {
  167.         var ev, i;
  168.         var len = events.length;
  169.         var a1 = args[0];
  170.         var a2 = args[1];
  171.         var a3 = args[2];
  172.  
  173.         switch (args.length) {
  174.         case 0:
  175.             for (i = 0; i < len; i++)
  176.                 (ev = events[i]).callback.call(ev.ctx);
  177.             return;
  178.         case 1:
  179.             for (i = 0; i < len; i++)
  180.                 (ev = events[i]).callback.call(ev.ctx, a1);
  181.             return;
  182.         case 2:
  183.             for (i = 0; i < len; i++)
  184.                 (ev = events[i]).callback.call(ev.ctx, a1, a2);
  185.             return;
  186.         case 3:
  187.             for (i = 0; i < len; i++)
  188.                 (ev = events[i]).callback.call(ev.ctx, a1, a2, a3);
  189.             return;
  190.         default:
  191.             for (i = 0; i < len; i++)
  192.                 (ev = events[i]).callback.apply(ev.ctx, args);
  193.         }
  194.     };
  195.  
  196.     var listenMethods = { listenTo: 'on', listenToOnce: 'once' };
  197.  
  198.     // Inversion-of-control versions of `on` and `once`. Tell *this* object to
  199.     // listen to an event in another object ... keeping track of what it's
  200.     // listening to.
  201.     each(listenMethods, function (implementation, method) {
  202.         Events[method] = function (obj, name, callback) {
  203.             var listeners = this._listeners || (this._listeners = {});
  204.             var id = obj._listenerId || (obj._listenerId = uniqueId('l'));
  205.             listeners[id] = obj;
  206.             if (typeof name === 'object') callback = this;
  207.             obj[implementation](name, callback, this);
  208.             return this;
  209.         };
  210.     });
  211.  
  212.     // Aliases for backwards compatibility.
  213.     Events.bind = Events.on;
  214.     Events.unbind = Events.off;
  215.  
  216.     return Events;
  217. });
  218.  
  219. // https://c.disquscdn.com/next/next-core/core/Events.js

Replies to Re: Re:Events.js rss

Title Name Language When
Re: Re: WindowBus.js.js disqus.com javascript 2 Months ago.

Reply to "Re: Re:Events.js"

Here you can reply to the paste above