Backbone event Events Subscribe to and publish source code small read

Source: Internet
Author: User

Nodejs has Eventemitter class, think backbone have an event module can do events binding and triggering, is the core module of backbone.

Backbone Event Module

    • On Add custom events
    • Off Delete custom events
    • Trigger Send a custom event
    • Once add a custom event that executes only once (internally dependent on _.once)
    • Listento Adding an Observation object
    • Listentoonce add an Observation object that executes only once
    • Stoplistening deleting an added observation object
------------------------------------
   On:function (name, callback, context) {      if (!eventsapi (this, ' on ', name, [callback, context]) | |!callback) return THI s;      this._events | | (this._events = {});      var events = This._events[name] | | (This._events[name] = []);      Events.push ({callback:callback, context:context, Ctx:context | | this});      return this;    },

The _events attribute is added to the current instance of Bancbone.event, and all time subscribed through backbone.events is found on this property.

    _events={        name:[{callback:function () {}, Context:object, Ctx:object}],        name:[{callback:function () {}, Context:object, Ctx:object}]    }

  

    Trigger:function (name) {      if (!this._events) return this;      var args = slice.call (arguments, 1);      if (!eventsapi (this, ' Trigger ', name, args)] return this;      var events = This._events[name];      var allevents = This._events.all;      if (events) triggerevents (events, args);      if (allevents) triggerevents (allevents, arguments);      return this;    }
Object {callback:function, context:object, Ctx:object}
  var triggerevents = function (events, args) {    var ev, i =-1, L = events.length, a1 = Args[0], a2 = args[1], a3 = args[ 2];    Switch (args.length) {case      0:while (++i < L) (ev = Events[i]). Callback.call (ev.ctx); return;      Case 1:while (++i < L) (ev = Events[i]). Callback.call (Ev.ctx, A1); return;      Case 2:while (++i < L) (ev = Events[i]). Callback.call (Ev.ctx, a1, A2); return;      Case 3:while (++i < L) (ev = Events[i]). Callback.call (Ev.ctx, A1, A2, A3); return;      Default:while (++i < L) (ev = Events[i]). callback.apply (Ev.ctx, args); return;    }  };

  

Backbone event Events Subscribe to and publish source code small read

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.