BaseApp.js

From disqus.com, 5 Days ago, written in JavaScript, viewed 3 times. This paste is a reply to Re: reporting.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/9d4397b5 Embed
Download Paste or View Raw
  1. define('core/apps/BaseApp',['require','core/Events','core/utils/object/extend','core/utils/object/has','core/utils/uniqueId'],function (require) {
  2.     'use strict';
  3.  
  4.     var Events = require('core/Events');
  5.     var extend = require('core/utils/object/extend');
  6.     var has = require('core/utils/object/has');
  7.     var uniqueId = require('core/utils/uniqueId');
  8.  
  9.     // The base application class for Disqus apps. It is a very "barebones"
  10.     // class having an `extend` method on it for easy inheritance analogous to
  11.     // Backbone classes' extend method. The class covers the following:
  12.     //    * Assigns a uid and and the passed settings to the instances
  13.     //    * Registers and unregisters the instance if the derived class has
  14.     //      `register` and `unregister` *classmethods*
  15.     //    * Supports the standard event interface from Backbone events:
  16.     //      on, once, off, listenTo, stopListening, trigger
  17.     //    * Assigns `event name: handler` mappings under `events` property using
  18.     //      this.on. It traverses up to the root class and binds all events
  19.     //      defined in all parent classes, in order. Thus an event handler on
  20.     //      the parent class will always be executed before a handler defined
  21.     //      for the same event on the child class. THIS IS SUPER IMPORTANT!
  22.     //    * Does all the "magic" explained above for one-time events listed
  23.     //      under `onceEvents` property using `this.once`.
  24.     //    * Detaches all event listeners and stops listening to anything when
  25.     //      destructor is called (destroy).
  26.     var BaseApp = function (settings) {
  27.         this.uid = uniqueId('dsq-app');
  28.  
  29.         this.settings = settings || {};
  30.  
  31.         var parents = [];
  32.         var parent = this.constructor.prototype;
  33.  
  34.         do {
  35.             parents.unshift(parent);
  36.             parent = parent.constructor.__super__;
  37.         } while (parent);
  38.  
  39.         // The event attaching order here is VERY important. It starts attaching
  40.         // events from the top most parent and comes down to self which allows
  41.         // proper inheritance: the handler on the base class is executed first
  42.         for (var i = 0, length = parents.length; i < length; i++) {
  43.             parent = parents[i];
  44.             if (has(parent, 'events'))
  45.                 this.on(parent.events, this);
  46.             if (has(parent, 'onceEvents'))
  47.                 this.once(parent.onceEvents, this);
  48.         }
  49.     };
  50.  
  51.     extend(BaseApp.prototype, Events);
  52.  
  53.     BaseApp.prototype.destroy = function () {
  54.         this.off();
  55.         this.stopListening();
  56.     };
  57.  
  58.     // taken from Backbone extend helper
  59.     BaseApp.extend = function (protoProps, staticProps) {
  60.         var parent = this; // eslint-disable-line consistent-this
  61.         var child;
  62.  
  63.         // The constructor function for the new subclass is either defined by you
  64.         // (the "constructor" property in your `extend` definition), or defaulted
  65.         // by us to simply call the parent's constructor.
  66.         if (protoProps && has(protoProps, 'constructor'))
  67.             child = protoProps.constructor;
  68.         else
  69.             child = function () { return parent.apply(this, arguments); };
  70.  
  71.         // Add static properties to the constructor function, if supplied.
  72.         extend(child, parent, staticProps);
  73.  
  74.         // Set the prototype chain to inherit from `parent`, without calling
  75.         // `parent`'s constructor function.
  76.         var Surrogate = function () { this.constructor = child; };
  77.         Surrogate.prototype = parent.prototype;
  78.         child.prototype = new Surrogate();
  79.  
  80.         // Add prototype properties (instance properties) to the subclass,
  81.         // if supplied.
  82.         if (protoProps) extend(child.prototype, protoProps);
  83.  
  84.         // Set a convenience property in case the parent's prototype is needed
  85.         // later.
  86.         child.__super__ = parent.prototype;
  87.  
  88.         return child;
  89.     };
  90.  
  91.     return BaseApp;
  92. });
  93.  
  94. // https://c.disquscdn.com/next/next-core/core/apps/BaseApp.js

Replies to BaseApp.js rss

Title Name Language When
WindowedApp.js disqus.com javascript 5 Days ago.

Reply to "BaseApp.js"

Here you can reply to the paste above