[Dojo] dojo. ready (dojo. addOnLoad) "Forward"

Source: Internet
Author: User

Recently, the dojo framework is used in the project. A large number of dojo. ready is used in each page to complete page initialization. But for some reason, you need to do something before all the dojo. ready.
This is a bit of a hassle before dojo 1.6 (including 1.6. View Source Code:
[Csharp]
Dojo. ready = dojo. addOnLoad = function (/* Object */obj,/* String | Function? */FunctionName ){
D. _ onto (d. _ loaders, obj, functionName );
 
If (d. _ postLoad & d. _ inFlightCount = 0 &&! D. _ loadpolicying ){
D. _ callLoaded ();
}
}

Dojo. ready = dojo. addOnLoad = function (/* Object */obj,/* String | Function? */FunctionName ){
D. _ onto (d. _ loaders, obj, functionName );

If (d. _ postLoad & d. _ inFlightCount = 0 &&! D. _ loadpolicying ){
D. _ callLoaded ();
}
} The actual initialization method is dojo. loaded:
[Csharp]
Dojo. loaded = function (){
 
D. _ loadpolicying = true;
D. _ postLoad = true;
Var mll = d. _ loaders;
 
// Clear listeners so new ones can be added
// For other xdomain package loads after the initial load.
D. _ loaders = [];
 
For (var x = 0; x <mll. length; x ++ ){
Mll [x] ();
}
 
D. _ loadpolicying = false;

If (d. _ postLoad & d. _ inFlightCount = 0 & mll. length ){
D. _ callLoaded ();
}
}

Dojo. loaded = function (){

D. _ loadpolicying = true;
D. _ postLoad = true;
Var mll = d. _ loaders;

// Clear listeners so new ones can be added
// For other xdomain package loads after the initial load.
D. _ loaders = [];

For (var x = 0; x <mll. length; x ++ ){
Mll [x] ();
}

D. _ loadpolicying = false;
 
If (d. _ postLoad & d. _ inFlightCount = 0 & mll. length ){
D. _ callLoaded ();
}
} The functions added through dojo. ready (addOnLoad) are stored in the array of dojo. _ loaders.
So we have to rewrite the loaded method:
[Csharp]
Dojo. _ inits = [];
Dojo. addInit = function (obj ){
Dojo. _ inits. push (obj );
};
 
Dojo. loaded = function (){
Var d = dojo;
D. _ loadpolicying = true;
D. _ postLoad = true;
 
Var mll = [];
For (var I = 0; I <d. _ loaders. length; I ++ ){
Console. log (d. _ loaders [I]);
Mll. push (d. _ loaders [I]);
If (d. _ loaders [I]. toString (). indexOf ('registerwin')>-1)
For (var j = 0; j <d. _ inits. length; j ++)
Mll. push (d. _ inits [j]);
}
}
 
D. _ loaders = [];
D. _ inits = [];
 
For (var x = 0; x <mll. length; x ++ ){
Mll [x] ();
}
 
D. _ loadpolicying = false;
If (d. _ postLoad & d. _ inFlightCount = 0 & mll. length ){
D. _ callLoaded ();
}
}

Dojo. _ inits = [];
Dojo. addInit = function (obj ){
Dojo. _ inits. push (obj );
};

Dojo. loaded = function (){
Var d = dojo;
D. _ loadpolicying = true;
D. _ postLoad = true;

Var mll = [];
For (var I = 0; I <d. _ loaders. length; I ++ ){
Console. log (d. _ loaders [I]);
Mll. push (d. _ loaders [I]);
If (d. _ loaders [I]. toString (). indexOf ('registerwin')>-1)
For (var j = 0; j <d. _ inits. length; j ++)
Mll. push (d. _ inits [j]);
}
}

D. _ loaders = [];
D. _ inits = [];

For (var x = 0; x <mll. length; x ++ ){
Mll [x] ();
}

D. _ loadpolicying = false;
If (d. _ postLoad & d. _ inFlightCount = 0 & mll. length ){
D. _ callLoaded ();
}
} I added the _ inits array to dojo and saved the method registered through addInit. Then, check all the methods added through dojo. ready/addOnLoad and insert the methods in _ inits after dijit completes registration (at this time, the dojo parse has been completed. In this way, I can add some processing in the framework through dojo. addInit so that they are always executed before the methods added by dojo. ready/addOnLoad.

Test:
[Javascript]
Dojo. ready (function () {console. log (1 );});
Dojo. ready (function () {console. log (2 );});
Dojo. addInit (function () {console. log ('init ');});

Dojo. ready (function () {console. log (1 );});
Dojo. ready (function () {console. log (2 );});
Dojo. addInit (function () {console. log ('init ');});
Result:
[Html] view plaincopyprint? Log: init
Log: 1
Log: 2

Log: init
Log: 1
Log: 2
Since dojo 1.7, most of dojo. js has been rewritten. The interfaces of dojo. ready have also changed:
[Csharp] view plaincopyprint? Var ready = dojo. ready = dojo. addOnLoad = function (priority, context, callback)

Var ready = dojo. ready = dojo. addOnLoad = function (priority, context, callback) is added with the priority parameter. The above implementation can be greatly simplified. The default priority is 1000.
The following code can be easily implemented:
[Csharp]
Dojo. addInit = function (obj) {dojo. ready (1, obj );};

Dojo. addInit = function (obj) {dojo. ready (1, obj );};


 

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.