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