Some common functions of dojo (5) -- event processing and Ajax I/O function extension

Source: Internet
Author: User

Every Ajax framework extends JavaScript and provides many common functions, enhancing the development efficiency of JavaScript. In this section
Common functions in. Because many common functions of dojo are used, these common functions are divided into five categories for your convenience. This article will introduce the fifth and last part
Capacity: event processing and Ajax I/O function extension.

* The vast majority of the content of this series of blog posts comes from the dojo reference on dojocampus.org.
Thanks to the translators Fei Jia, Zhu Xiao Wen, li wen Bing, Zhang Jun,
Hu Kuang, Huang Wei, Wu Min Qi, Mo Ying, Cheng Fu, Zhong Si Qi

Dojo. Connect, dojo. Disconnect

Dojo. connect is used to bind a specified event handler to an event. It can also be bound to a function. After the bound function is executed, the given function will be triggered for execution. Dojo. Connect is the most important event processing and delegation method of dojo. It can receive five parameters:

 

Dojo. Connect (OBJ, event, context, method, dontfix );

 

  • OBJ: The Event source object or the scope of the bound function. The default value (or obj is set to null) is Dojo. Global.
  • Event: event name or bound function name. Combined with the first parameter, the bound event (or function) is specified as OBJ [event].
  • Context: the scope of event processing functions (binding functions. The default value (or the context is set to null) is Dojo. Global.
  • Method: The name or reference of the event processing function (bound function). If it is a function name and combined with the context parameter, the function is specified as context [Method]. This function will be triggered after the event or function binding is executed. Parameters received by the function are the same as those of the event or bound function.
  • Dontfix: This is an optional parameter. If the first parameter obj is a DOM Node object and the dontfix is set to true, event processing will not be delegated to the DOM event manager for distribution.

Dojo. connect will return a handle object. It is strongly recommended that you save these handle objects in the Code and call dojo when the code is destroyed. disconnect to destroy these connections. Otherwise, memory leakage may occur. For more information, see the following example:

 

// When obj. after onchange () is executed, the UI is called. update (): <br/> dojo. connect (OBJ, "onchange", UI, "Update"); <br/> dojo. connect (OBJ, "onchange", UI, UI. update); <br/> // call disconnect to destroy the created connection <br/> var link = dojo. connect (OBJ, "onchange", UI, "Update"); <br/>... <br/> dojo. disconnect (Link); <br/> // when onglobalevent is executed, watcher is called. handler: <br/> dojo. connect (null, "onglobalevent", watcher, "handler"); <br/> // In ob. after oncustomevent is executed, call customeventhandler: <br/> dojo. connect (OB, "oncustomevent", null, "customeventhandler"); <br/> dojo. connect (OB, "oncustomevent", "customeventhandler"); <br/> // In ob. after oncustomevent is executed, call customeventhandler <br/> // The event handler functions in the two examples have the same scope (this): <br/> dojo. connect (OB, "oncustomevent", null, customeventhandler); <br/> dojo. connect (OB, "oncustomevent", customeventhandler); <br/> // After globalevent is executed, call globalhandler: <br/> dojo. connect (null, "globalevent", null, globalhandler); <br/> dojo. connect ("globalevent", globalhandler); <br/>

Dojo. subscribe, dojo. Unsubscribe

Dojo. subscribe is used to register a function to listen to a published channel. When dojo. Publish is called to send data to the monitored channel, the registered listening function is triggered and the received data is used as the parameter. For more information, see the following example:

 

// Register the listener function and listen to the "/Foo/BAR/Baz" channel <br/> dojo. subscribe ("/Foo/BAR/Baz", function (data) {<br/> console. log ("I got", data); <br/>}); <br/> // call dojo when you want to trigger the registered listening function. publish sends data to the "/Foo/BAR/Baz" channel <br/> dojo. publish ("/Foo/BAR/Baz", [{some: "Object Data"}]); <br/> // The channel names can be any defined strings <br/> dojo. subscribe ("foo-bar", function (data) {/* handle */}); <br/> dojo. subscribe ("bar", function (data) {/* handle */}); <br/> dojo. subscribe ("/Foo/bar", function (data) {/* handle */}); <br/>

 

And dojo. same as connect, dojo. subscribe returns an object. You must call dojo to destroy the code. unsubscribe, pass in Dojo. the objects returned by subscribe are destroyed to avoid Memory leakage.

Dojo. Publish, dojo. connectpublisher

Combined with the above dojo. subscribe, dojo. Publish is a function used to publish data to a specified channel.

Dojo. connectpublisher is used to bind certain events to automatically publish data to the specified channel. As shown in the following example, the functions of the two code segments are the same:

 

Dojo. connect (myobject, "myevent", function () {<br/> dojo. publish ("/Some/topic/Name", arguments); <br/> });

 

Use dojo. connectpublisher:

 

Dojo. connectpublisher ("/Some/topic/Name", myobject, "myevent ");

 

Dojo. connectpublisher returns a handle object. When destroying code, you need to call dojo. Disconnect to destroy the connection to avoid Memory leakage.

Dojo. xhr

Note that dojo. xhr is not a function, and xhr is short for XMLHTTP request object. Dojo encapsulates a series of xhr functions for Ajax interaction, including: dojo. xhrget, dojo. xhrpost, dojo. xhrdelete, dojo. xhrput, dojo. rawxhrpost, and dojo. rawxhrput. The functions and usage of these functions are similar. Below is an example of simple usage of dojo. xhrget:

 

VaR queryobj = {sort: "ID"}; <br/> var xhrhandler = dojo. xhrget (<br/>{< br/> URL: "test. PHP ", <br/> content: queryobj <br/>}< br/>); <br/> xhrhandler. addcallback (function (data) {<br/> datahandler (data); <br/>}); <br/> xhrhandler. adderrback (function (error) {<br/> errorhandler (error); <br/> });

 

Dojo. xhr * receives a JSON object as a parameter, which can contain many attributes. The following lists some important attributes:

  • URL-The xhr Data Request receiving URL. Due to xhr security restrictions, this URL must be in the same domain and port as the script;
  • Timeout-the timeout value, in ms. When the wait time exceeds the specified value, an error is thrown to the specified error processing callback function;
  • Specifies whether the xhr request is synchronous or asynchronous. The default value is false (asynchronous );
  • Content-a json object whose key-value pairs are added as request parameters to the URL;

Only a part of the attributes are listed. For the list of all attributes, see the dojo campus document (Dojo. xhrget
, Dojo. xhrpost
). The following is another example:

 

// Send a POST request and ignore response <br/> dojo. xhrpost ({<br/> form: "someformid", // when a POST request is sent, the request data is provided by Form "someformid, URL can be obtained through the action attribute of form <br/> Timeout: 3000, <br/> content: {part: "One", another: "part"} // creates? Part = one & Another = part <br/>}); <br/> // obtain JSON data <br/> dojo. xhrget ({<br/> URL: "data. JSON ", <br/> handleas:" JSON ", <br/> load: function (data) {<br/> for (var I in data) {<br/> console. log ("key", I, "value", data [I]); <br/>}< br/> })

 

So far, this series of articles: some common functions of dojo have come to an end. I hope these simple introductions will help you with your development, if you have any mistakes, please do not correct them.

Related Article

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.