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