JavaScript asynchronous Programming (ii) Distributed events

Source: Internet
Author: User
Tags emit

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

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.