JavaScript custom events without MVC, are bullying

Source: Internet
Author: User
Tags event listener

Custom events This has to be done with MVC.

The baby and I said, all write events without MVC, are bullying. I think it's a very deep thought.

In fact, the baby only said the second half sentence.

Event this thing, from the implementation, we need to implement the delegate registration management and event trigger callback two pieces, from the definition, the Event service needs to define the delegate management and event triggering, the event listener defines the delegate registration and event callback, from the role division, the need to be divided into service, trigger, listener. This set of permutations, you've been messed up, right? So the events are of this virtue:

Server public delegate void EventHandler (object sender, EventArgs e);p ublic event EventHandler someeventhandler;protected virtual void onsomeevent (EventArgs e) {  if (someeventhandler) {    Someeventhandler (this, e);  }} Trigger Onsomeevent (eventargs.instance);//listener Someeventhandler + = (s, e) + = {};

OK, so this is the definition, given to C # to define. In other words, this EventArgs class is used very vivid ah, save to change long ginseng ~ With such a heavy language definition is the most concise.

So the baby is right, you have to use MVC.

Therefore, defining the event Service first is fundamental:

App.service (' Events ', function () {    var eventlist = {};    This.on = function (event, callback) {      if (!eventlist[event]) {        eventlist[event] = [];      }      Eventlist[event].push (callback);      return this;    };    this.un = function (event) {      for (var i = 0, L = eventlist[event].length; i < L; i++) {        Eventlist[event][i] = null;      }      Delete Eventlist[event];      return this;    };    This.trigger = function (event) {      if (Eventlist[event]) {        var args = Array.prototype.slice.call (arguments). Slice (1);        for (var i = 0, L = eventlist[event].length; i < L; i++) {          eventlist[event][i].apply (null, args);        }      }      return this;    };  })

The delegate management is finished, the event trigger is completed:

Events.trigger (' Event1 ', ' Para1 ', ' Para2 '). Trigger (' Event2 ', ' Para1 ');

Then there is the delegate registration and the event callback:

Events.on (' Event1 ', function (e, f) {  //Operation E F}). On (' Event2 ', function (e) {    //operation e  });

This is done. It can't be simpler.

---------------------------------------

The Purple cuckoo is still sweet.

The Hong Kong version of Iphone6 next month will be almost.

6P problem too many, even their own system applications are pits. If this product has no follow-up ... Anyway, 6P is not needed.

JavaScript custom events without MVC, are bullying

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.