]>
Commit | Line | Data |
---|---|---|
f0cfe83e AD |
1 | define("dojo/ready", ["./_base/kernel", "./has", "require", "./domReady", "./_base/lang"], function(dojo, has, require, domReady, lang){ |
2 | // module: | |
3 | // dojo/ready | |
4 | // note: | |
5 | // This module should be unnecessary in dojo 2.0 | |
6 | ||
7 | var | |
8 | // truthy if DOMContentLoaded or better (e.g., window.onload fired) has been achieved | |
9 | isDomReady = 0, | |
10 | ||
11 | // a function to call to cause onLoad to be called when all requested modules have been loaded | |
12 | requestCompleteSignal, | |
13 | ||
14 | // The queue of functions waiting to execute as soon as dojo.ready conditions satisfied | |
15 | loadQ = [], | |
16 | ||
17 | // prevent recursion in onLoad | |
18 | onLoadRecursiveGuard = 0, | |
19 | ||
20 | handleDomReady = function(){ | |
21 | isDomReady = 1; | |
22 | dojo._postLoad = dojo.config.afterOnLoad = true; | |
23 | if(loadQ.length){ | |
24 | requestCompleteSignal(onLoad); | |
25 | } | |
26 | }, | |
27 | ||
28 | // run the next function queued with dojo.ready | |
29 | onLoad = function(){ | |
30 | if(isDomReady && !onLoadRecursiveGuard && loadQ.length){ | |
31 | //guard against recursions into this function | |
32 | onLoadRecursiveGuard = 1; | |
33 | var f = loadQ.shift(); | |
34 | try{ | |
35 | f(); | |
36 | } | |
37 | // FIXME: signal the error via require.on | |
38 | finally{ | |
39 | onLoadRecursiveGuard = 0; | |
40 | } | |
41 | onLoadRecursiveGuard = 0; | |
42 | if(loadQ.length){ | |
43 | requestCompleteSignal(onLoad); | |
44 | } | |
45 | } | |
46 | }; | |
47 | ||
48 | require.on("idle", onLoad); | |
49 | requestCompleteSignal = function(){ | |
50 | if(require.idle()){ | |
51 | onLoad(); | |
52 | } // else do nothing, onLoad will be called with the next idle signal | |
53 | }; | |
54 | ||
55 | var ready = dojo.ready = dojo.addOnLoad = function(priority, context, callback){ | |
56 | // summary: | |
57 | // Add a function to execute on DOM content loaded and all requested modules have arrived and been evaluated. | |
58 | // In most cases, the `domReady` plug-in should suffice and this method should not be needed. | |
59 | // priority: Integer? | |
60 | // The order in which to exec this callback relative to other callbacks, defaults to 1000 | |
61 | // context: Object?|Function | |
62 | // The context in which to run execute callback, or a callback if not using context | |
63 | // callback: Function? | |
64 | // The function to execute. | |
65 | // | |
66 | // example: | |
67 | // Simple DOM and Modules ready syntax | |
68 | // | require(["dojo/ready"], function(ready){ | |
69 | // | ready(function(){ alert("Dom ready!"); }); | |
70 | // | }); | |
71 | // | |
72 | // example: | |
73 | // Using a priority | |
74 | // | require(["dojo/ready"], function(ready){ | |
75 | // | ready(2, function(){ alert("low priority ready!"); }) | |
76 | // | }); | |
77 | // | |
78 | // example: | |
79 | // Using context | |
80 | // | require(["dojo/ready"], function(ready){ | |
81 | // | ready(foo, function(){ | |
82 | // | // in here, this == foo | |
83 | // | }); | |
84 | // | }); | |
85 | // | |
86 | // example: | |
87 | // Using dojo/hitch style args: | |
88 | // | require(["dojo/ready"], function(ready){ | |
89 | // | var foo = { dojoReady: function(){ console.warn(this, "dojo dom and modules ready."); } }; | |
90 | // | ready(foo, "dojoReady"); | |
91 | // | }); | |
92 | ||
93 | var hitchArgs = lang._toArray(arguments); | |
94 | if(typeof priority != "number"){ | |
95 | callback = context; | |
96 | context = priority; | |
97 | priority = 1000; | |
98 | }else{ | |
99 | hitchArgs.shift(); | |
100 | } | |
101 | callback = callback ? | |
102 | lang.hitch.apply(dojo, hitchArgs) : | |
103 | function(){ | |
104 | context(); | |
105 | }; | |
106 | callback.priority = priority; | |
107 | for(var i = 0; i < loadQ.length && priority >= loadQ[i].priority; i++){} | |
108 | loadQ.splice(i, 0, callback); | |
109 | requestCompleteSignal(); | |
110 | }; | |
111 | ||
112 | 1 || has.add("dojo-config-addOnLoad", 1); | |
113 | if( 1 ){ | |
114 | var dca = dojo.config.addOnLoad; | |
115 | if(dca){ | |
116 | ready[(lang.isArray(dca) ? "apply" : "call")](dojo, dca); | |
117 | } | |
118 | } | |
119 | ||
120 | if( 1 && dojo.config.parseOnLoad && !dojo.isAsync){ | |
121 | ready(99, function(){ | |
122 | if(!dojo.parser){ | |
123 | dojo.deprecated("Add explicit require(['dojo/parser']);", "", "2.0"); | |
124 | require(["dojo/parser"]); | |
125 | } | |
126 | }); | |
127 | } | |
128 | ||
129 | if( 1 ){ | |
130 | domReady(handleDomReady); | |
131 | }else{ | |
132 | handleDomReady(); | |
133 | } | |
134 | ||
135 | return ready; | |
136 | }); |