Scope of extjs Ajax callback Functions

Source: Internet
Author: User

A panel in extjs calls Ext. when Ajax is enabled, the callback is successful and the method specified by success is executed. The scope of this method will be changed to the top web page, so that the callback method cannot pass this. items. get () to find the corresponding control, only through Ext. getcmp () is searched from the entire web page, so that controls with the same ID will be modified.

Before extjs 4, the scope of the callback function should be changed through delegation, and the createdelegate method should be used.

In extjs 4.0.7, use Ext. function. bind to modify the scope of the callback function.

BIND (function FN, [object scope], [array ARGs], [Boolean/number appendargs]): Function

 

1 /*
2 Request: Incoming bytes
3 successcallback: callback method executed after the background execution is successful
4 failurecallback: the callback method executed after the background execution fails.
5 Caller: the form that calls the accessdata method. With this parameter, you can access the variables in the call form in the callback method.
6. When calling this method, if the preceding parameter is not required, but the following parameter is required, the preceding parameter must be input null.
7 */
8 function accessdata (request, successcallback, failurecallback, caller)
9 {
10 // set Content-Type to application/JSON format
11 Ext. Ajax. defaultpostheader = 'application/json ';
12
13 // unified callback function for access failure
14 var onfailure = function (R, opts)
15 {
16 alert (R. responsetext );
17}
18
19 var onsuccess = function (response, options)
20 {
21 var OBJ = eval ('+ response. responsetext + ')');
22
23 if (0 = obj. accessdataresult. Result)
24 {
25 if (null! = Successcallback)
26 {
27 if (null = caller)
28 {
29 successcallback (obj. accessdataresult. outmsg );
30}
31 else
32 {
33 // define the scope of the callback function as the caller
34 var successcallbackdelegate = ext. function. BIND (successcallback, caller );
35 successcallbackdelegate (obj. accessdataresult. outmsg );
36}
37}
38}
39 else if (99 = obj. accessdataresult. Result)
40 {
41 initlogin ();
42}
43 else
44 {
45 if (null! = Failurecallback)
46 {
47 If (null = caller)
48 {
49 failurecallback (obj. accessdataresult. outmsg );
50}
51 else
52 {
53 var failurecallbackdelegate = ext. function. BIND (failurecallback, caller );
54 failurecallbackdelegate (obj. accessdataresult. outmsg );
55}
56}
57 else
58 {
59 alert (obj. accessdataresult. outmsg );
60}
61}
62}
63
64 // If request. parm. parms is a JSON object, you must first convert it to a string,
65 // mainly because of the background WCF interface, only string parameters are accepted. If a JSON object is directly input,
66 // The background parameter must be defined as an object, which is not easy to parse
67 request. parm. parms = ext. JSON. encode (request. parm. parms );
68
69 ext. Ajax. Request (
70 {
71 URL: './WCF/examplewcf. svc/accessdata', // method address to be accessed
72 method: 'post ',
73 Params: Ext. JSON. encode (request), // encode the input parameters in JSON format.
74 scope: caller, // cache callback function Scope
75 success: onsuccess,
76 failure: onfailure
77 });
78}

 

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.