Dojo learning and using (04), publishing/booking communication, built-in observer mode of dojo (Dojo. Publish and dojo. subscribe)

Source: Internet
Author: User

 

Dojo. Publish and dojo. subscribe

 

// Dojo. Publish and dojo. Subscribe: simple and powerful observer Mode Implemented by dojo

Publish/subscribe communication

Although direct "connection" communication provided by dojo. Connect can solve many problems, it is more suitable for indirect "broadcast" communication when multiple components are needed for anonymous communication.

In these cases, you can use dojo. Publish and dojo. subscribe.

A typical example is to implement communication between a JavaScript Object and other objects based on a one-to-many relationship. In this case, you can establish and manage multiple dojo with high cohesion. compared with the connect connection, a simpler solution should be to publish a notification when a part occurs (colleagues can also transmit data ), this allows other parts to subscribe to this notification and automatically perform corresponding operations when an event occurs. The advantage of this method is that the broadcast owner does not need to know what objects have subscribed to notifications, or even whether other objects exist. [typical observer mode in design mode]

 

Publish/reserve an API for communication. (The context parameter can be omitted when the dojo. subscribe method is called. This method can internally process the parameter "same as the dojo. Connect Method" as we would like ")

Dojo. Publish API

 // Event Publishing
Dojo. Publish (/* string */topic,/* array */args)
// Topic event topic and event parameters published by ARGs

// Event subscription
Dojo. subscribe (/* string */topic,
/* Object | null */context,
/* String | function */method) // returns a handle object
// The topic of the event to be subscribed,

// Unsubscribe and unsubscribe
Dojo. Unsubscribe (/* handle */handle)
// Handle is the handle of the event to be unsubscribed.

// [Note: like the handle object returned by the dojo. Connect object, the handle object returned by dojo. subscribe can also be regarded as a black box, which is only useful when unsubscribing]

 

 

The following is an example of using dojo. Publish and dojo. Subscribe:

Dojo. Publish and dojo. subscribe

 
Function Tom (topic ){
This. Topic = topic;
This. Hello = function (){
Alert ("Hello, I'm Tom! ");

// Tom directly publishes information, not specific to a specific target
Dojo. Publish (this. Topic );
}
}

Function Jerry (topic ){
This. Topic = topic;
This. Hello = function (){
Alert ("Hello, I'm Jerry ");
}

// Jerry directly subscribes to the information, but does not apply to a specific source
Dojo. subscribe (this. topic, this, "hello ");
}

VaR Tom = new Tom ("/Hello ");
VaR Jerry = new Jerry ("/Hello ");

Tom. Hello ();

The first method Tom first published the topic in publish, and the second method Jerry subscribed to the method in subscribe.
[You should note that the last 2nd rows and the last 3rd rows have an event topic as a string and the start of the string is a forward slash. This is a recommended method for dojo. It is agreed to add a forward slash before the topic name. Because the forward slash is not commonly used in Javascript, it is easy to recognize the topic name]

 

 

Dojo. connect can only establish connections between specific sources and specific targets, and publish/subscribe communication uses the broadcast method to send notifications (or publish theme events) there are no restrictions on the source and target for receiving notifications (or registering and subscribing to events. The reason for the strong loosely coupled architecture is that it can be understood as a collection of related plug-ins with the lowest investment and the simplest design.

In addition, you can also provide the second parameter for publish. This parameter must be an array [array]. The array will be finally received by the subscribe handler in the form of a single named parameter.Accept!

[[A common error is that the data that is not passed to dojo. Publish must be placed in an array, and the dojo. subscribe handler receives these parameters in the form of a single parameter]]
The following is a slight modification to demonstrate how to cancel the subscription of dojo. Unsubscribe.

 

Dojo. Unsubscribe unsubscribe

 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<SCRIPT type = "text/JavaScript" src = "JS/dojo. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
Function Tom (topic ){
This. Topic = topic;
This. Hello = function (){
Alert ("Hello, I'm Tom! ");

// Tom directly publishes information, not specific to a specific target
Dojo. Publish (this. Topic );
}
}

Function Jerry (topic ){
This. Topic = topic;
This. Hello = function (){
Alert ("Hello, I'm Jerry ");

// Cancel subscription, handle
Dojo. Unsubscribe (this. Handle );
}

// Dojo. subscribe returns a handle
This. Handle = dojo. subscribe (this. topic, this, "hello ");
}

VaR Tom = new Tom ("/Hello ");
VaR Jerry = new Jerry ("/Hello ");

</SCRIPT>
</Head>
<Body>

<Button onclick = "Tom. Hello ();"> click me </button>

</Body>
</Html>

 

 

When you click the button for the first time, Tom's "Hello, I'm Tom!" will pop up! ", And Jerry's" Hello, I'm Jerry ",

Then click the button. Because the subscription is canceled, only Tom's "Hello, I'm Tom!" will pop up! ", But Jerry won't respond to the/Hello topic any more, because he has canceled his subscription to this topic.

 

 

Dojo. connectpublisher

 

Finally, there is another possibility. Suppose you are not sure whether you can modify Tom's hello method to include. publish calls may be caused by external restrictions that prohibit such operations, or the code is not compiled by you at the beginning. However, you do not need to worry about it in any case. At this time, you can use another method, is dojo. connectpublisher, which is responsible for releasing notifications to us at specific times:

function Tom() {
this.hello = function () {
alert("Hello,I'm Tom!");
}
}

function Jerry() {
this.greet = function () {
alert("Hello,I'm Jerry");
}
}

var tom = new Tom();
var jerry = new Jerry();

var topic = "/SayHi";

dojo.subscribe(topic, jerry, "greet");
dojo.connectPublisher(topic, tom, "hello");

tom.hello();

[Note: in fact, the background working principle of connectpublisher is to establish a connection between the function call and the dojo. Publish call through dojo. Connect whenever a specific function is called]

 

 

In the previous article and this article, we should be able to achieve the following learning objectives:

Summary:

1. Understand that dojo. Connect can standardize the event objects in the passed-in time processing program, thus providing cross-platform porting capability.

2. Understand how to use dojo. Connect to connect any dom time, JavaScript Object events, and common functions to create event-driven response modes,

3. Establish a connection using the release/Reservation notification and implement a loosely coupled communication architecture in the application.

4. Understand the advantages and disadvantages of using dojo. Connect in the application architecture design and publishing dojo. Publish/to book the communication between dojo. subscribe.

 

 

Through learning, I think dojo is still very good. Although I have been familiar with and understand jquer with jquery before, the Object-Oriented function of dojo is more powerful,

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.