Dojo trial Note 3-asynchronous communication [original]

Source: Internet
Author: User

Since dojo is a framework of Ajax, asynchronous communication is essential; otherwise, it is called Ajax. In dojo, this part is in the dojo. Io package.
Let's take a look at this section:

1 // an asynchronous request to foo. php that returns a javascript literal
2 // which is eval () 'd
3 var bindargs = {
4 URL: "foo. php ",
5 mimetype: "text/JavaScript ",
6 error: function (type, errobj ){
7 // handle error here
8 },
9 load: function (type, Data, EVT ){
10 // handle successful response here
11}
12 };
13
14 // dispatch the request
15 var requestobj = dojo. Io. BIND (bindargs );
16
17
18 // of course, it can be written
19 // dojo. Io. BIND ({
20 // URL: "t4.htm ",
21 // mimetype: "text/JavaScript ",
22 // error: function (type, errobj ){
23 /// handle error here
24 //},
25 // load: function (type, Data, EVT ){
26 /// handle successful response here
27 //}});
28

Dojo. io. in the BIND (requestobject) method, only one parameter is the request object. The request object can be dojo. io. request. You can also specify the dojo. io. an anonymous object of the basic request attribute. In the above Code, we do not have a new dojo. Io. Request object. Therefore, we use anonymous objects. In applications, anonymous objects are commonly used.
From the code of the above anonymous object, we can see that the dojo. Io. Request object has two attributes and two methods:
1. "url" indicates the requested resource address.
2. "mimetype", response content type. The default value is "text/plain". Note the difference between mimetype and HTTP transmission. When mimetype is "text/JavaScript, request and execute the Javascript script.
3. The "error" method indicates the processing when the request fails.
4. "LOAD" method, the operation after successful request.
The default sync attribute of the dojo. Io. Request object is false. That is to say, it is not synchronized (that is, asynchronous). After a request is sent, subsequent operations can be continued, which is called asynchronous operations. Once the value is true, the request must be processed before subsequent operations can continue. Otherwise, the operation will be blocked.
The above Code does not specify the sync attribute, that is, it is the default value of false, so it is executed asynchronously.
The dojo. Io. Request object has several attributes, which are described as follows:
Method: it is believed that the form can be used by many people. By default, it means "Post" or "get". The default value is "get ".
Formnode: A Dom node. specifying a form will be submitted by this request. It will inherit the URL and method attributes above. If no value is assigned to the method Attribute before, the method attribute of the form will be taken. The value of this attribute is document. getelementbyid ("form1 ").
Content: Key-value pairs, which are added to parameters, are transmitted, which is equivalent to get and post data in HTTP transmission mode.
Transport: Specifies the transmission object used for this transmission. Once the specified transmission object is unavailable, the request will fail and an error event will be triggered. If this parameter is not specified, the BIND () method tries to find the most suitable available transport object from the registered transport Object List to execute the request. To use XMLHTTP to execute a request, you must specify transport: "xmlhttptransport ". Below is the code section for BIND () to try to find the most suitable transfer object:

1 For (VAR x = 0; x <dojo. Io. Transports. length; X ++)
2 {
3 var TMP = dojo. Io. Transports [x];
4 If (this [TMP]) & (this [TMP]. canhandlerequest )))
5 {
6 tsname = TMP;
7}
8}

Changeurl: after the request is complete, whether or not the page that sends the request will jump to a certain anchor. Although the official document explains it as 'boolean' type, the default value is 'false', it can also be a custom string, that is, the name of the anchor to jump. If this parameter is set to true, the anchor name is generated based on the current time. I am not sure about the usefulness of this parameter. However, you can specify the anchor name to jump to the expected anchor. Note that no matter whether the request is processed successfully or not, a jump occurs. In the official website documentation, it is said that in Mozilla/Firefox, the URL change will fail, and this "This may be removed in the future as it pertains exclusively to In-browser HTTP transports. ", will be removed in the future, because the combination with the transmission object is too proprietary. Example: If this parameter is set to true, the jump address may change to http: // 192.168.0.168/t3.htm #1144207896421. The following 1144207896421 is generated based on the timestamp. If it is set to changeurl: "AA", the address will be http: // 192.168.0.168/t3.htm # Aa.
Usecache: boolean type. The default value is false, indicating whether to cache the results of this request. Later, you can directly obtain the results of this request from the cache. If this parameter is set to true, the request is directly read from the cache. After BIND () is executed, we can immediately execute alert. Of course, asynchronous execution is required. When a new request is sent, the subsequent alert statements are executed first and then the request results are executed. After the same request is sent, because the result is in the cache, the request result is taken directly, so we can see that alert is executed after the execution is complete.
Bindsuccess: boolean type. The default value is false, indicating whether the request can be accepted and executed by other transmission objects. I am not sure about the specific usage. I will try again later.
The following describes how to use the dojo. Io. Request object.
The error () and load () methods have been discussed above. Pay attention to the parameters of these two methods. The error (type, errorobject) method has two parameters. The first parameter indicates the processing result. In error (), it is always "error", and the second parameter indicates the transmission details. Load (type, Data, event) has three parameters. The first parameter is the processing result. The value of load () is always "LOAD", indicating that the processing is successful, the second digit indicates the information returned after processing is successful, and the third digit indicates the underlying transmission object that can process the transmission details. An example on the official website is: When dojo is used. io. when xmlhttptransport is transmitted, the third parameter indicates a reference to the XMLHTTP object that executes the request.
There are two methods in the dojo. Io. Request object. Handle (type, Data, event) can be used to handle all situations, such as load (), error (), and other situations. When type = "LOAD", the three parameters are the same as load (). When type = "error", data indicates errorobject in error, the event is invalid. The following example uses handle () (from the official website ):

1 dojo. Io. BIND ({
2 URL: "http://foo.bar.com/sampleData.txt ",
3 handle: function (type, Data, EVT ){
4 If (type = "LOAD "){
5 // do something with the Data Object
6} else if (type = "error "){
7 // here, "data" is our error object
8 // respond to the error here
9} else {
10 // other types of events might get passed, handle them here
11}
12 },
13 mimetype: "text/plain"
14 });

From the above, you can easily understand the usage of handle.
The last method in the dojo. Io. Request object is abort (), which literally terminates the request and is used to interrupt the execution of the request to be processed. This behavior is owned by the transmission object that executes the request.
In Io. JS, you can find this line of dojo. Io. Transports = [];

From the above BIND () to find the most suitable transfer object process, we know that this is used to store registered transfer objects. This is an array where the transmission object will be referenced by every BIND request, and the first transmission object will accept a special request and process it (this sentence is not found a suitable example, understanding is not thorough enough. I will make it up later ).
Dojo. io. transports has an addtransport (name) method, which is used to register a transmission object that can be used to process requests. Note that the name must be in Dojo. io namespace, such as the commonly used dojo. io. xmlhttptransport, used for registration. io. transports. addtrasnport ("xmlhttptransport. io. the request object also said that to use XMLHTTP to execute the request, you must specify transport: "xmlhttptransport", which exactly matches.
According to the above information, transport can be written by myself, and then define what you need. I don't know how to write it, but I personally feel like inheritance in Java, very attractive results.

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.