Distributed events
Publish/Subscribe Mode distribution Events
? PubSub mode
The browser allows the addition of event handlers to DOM elements;
Node's Eventemitter object
Emitter.on (' Evacuate ', function () {...});
Emitter.emit (' evacuate ');
Emit is the trigger that is responsible for invoking all processors of the given event type
Create your own pubsub
Pubsub.on = function (EventType, handler) {
if (! ( EventType in This.handlers)) {
This.handlers[eventtype] = handler;
return this;
}
};
Pubsub.emit = function (EventType) {
var Handlerargs = Array.prototype.slice.call (arguments, 1);
for (var i =0; i < this.handlers[eventtype].length; i++) {
This.handlers[eventtype][i].apply (this, Handlerargs);
}
return this;
};
jquery namespace Event
If you bind both the name CLICK.TBB and the HOVER.TBB two events, simply call Unbind ('. TBB ') to unbind them at the same time.
jquery also supports the use of spaces to separate multiple events to bind or trigger multiple event types at the same time.
Synchronization of
The pubsub pattern is an important technique for handling asynchronous events, but there is nothing inherently related to asynchrony.
PubSub mode simplifies the naming, distribution, and stacking of events.
? The event-based model
As long as the object has an PubSub interface, it can be called an event object.
Special cases occur when the object used to store data is published as a result of content changes, and the object used to store the data is called a model.
Old-fashioned JavaScript relies on the processor of the input event to change the DOM directly.
The new JavaScript changes the model first, and then the model triggers the event, causing the DOM to update.
jquery Custom Events
In jquery, you can use the trigger method to trigger any desired event based on any DOM element.
Bubbling technology: As long as a DOM element triggers an event, its parent will then trigger the event, then the parent element of the parent element, and so on, back to the root element.
Only the Stoppropagation method that invokes the event during the bubbling process will stop.
The flaw in PubSub is that it does not apply to one-time events, and one-time events require different handling of the two outcomes of a one-time task performed by an asynchronous function (task success or failure). For example, an AJAX request. The tool used to solve the one-time event problem is called promise.
JavaScript asynchronous Programming (ii) Distributed events