[483] | 1 | define(["./_base/kernel", "./has", "require", "./has!host-browser?./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 | // The queue of functions waiting to execute as soon as dojo.ready conditions satisfied |
---|
| 12 | loadQ = [], |
---|
| 13 | |
---|
| 14 | // prevent recursion in onLoad |
---|
| 15 | onLoadRecursiveGuard = 0, |
---|
| 16 | |
---|
| 17 | handleDomReady = function(){ |
---|
| 18 | isDomReady = 1; |
---|
| 19 | dojo._postLoad = dojo.config.afterOnLoad = true; |
---|
| 20 | onEvent(); |
---|
| 21 | }, |
---|
| 22 | |
---|
| 23 | onEvent = function(){ |
---|
| 24 | // Called when some state changes: |
---|
| 25 | // - dom ready |
---|
| 26 | // - dojo/domReady has finished processing everything in its queue |
---|
| 27 | // - task added to loadQ |
---|
| 28 | // - require() has finished loading all currently requested modules |
---|
| 29 | // |
---|
| 30 | // Run the functions queued with dojo.ready if appropriate. |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | //guard against recursions into this function |
---|
| 34 | if(onLoadRecursiveGuard){ |
---|
| 35 | return; |
---|
| 36 | } |
---|
| 37 | onLoadRecursiveGuard = 1; |
---|
| 38 | |
---|
| 39 | // Run tasks in queue if require() is finished loading modules, the dom is ready, and there are no |
---|
| 40 | // pending tasks registered via domReady(). |
---|
| 41 | // The last step is necessary so that a user defined dojo.ready() callback is delayed until after the |
---|
| 42 | // domReady() calls inside of dojo. Failure can be seen on dijit/tests/robot/Dialog_ally.html on IE8 |
---|
| 43 | // because the dijit/focus.js domReady() callback doesn't execute until after the test starts running. |
---|
| 44 | while(isDomReady && (!domReady || domReady._Q.length == 0) && (require.idle ? require.idle() : true) && loadQ.length){ |
---|
| 45 | var f = loadQ.shift(); |
---|
| 46 | try{ |
---|
| 47 | f(); |
---|
| 48 | }catch(e){ |
---|
| 49 | // force the dojo.js on("error") handler do display the message |
---|
| 50 | e.info = e.message; |
---|
| 51 | if(require.signal){ |
---|
| 52 | require.signal("error", e); |
---|
| 53 | }else{ |
---|
| 54 | throw e; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | onLoadRecursiveGuard = 0; |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | // Check if we should run the next queue operation whenever require() finishes loading modules or domReady |
---|
| 63 | // finishes processing it's queue. |
---|
| 64 | require.on && require.on("idle", onEvent); |
---|
| 65 | if(domReady){ |
---|
| 66 | domReady._onQEmpty = onEvent; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | var ready = dojo.ready = dojo.addOnLoad = function(priority, context, callback){ |
---|
| 70 | // summary: |
---|
| 71 | // Add a function to execute on DOM content loaded and all requested modules have arrived and been evaluated. |
---|
| 72 | // In most cases, the `domReady` plug-in should suffice and this method should not be needed. |
---|
| 73 | // |
---|
| 74 | // When called in a non-browser environment, just checks that all requested modules have arrived and been |
---|
| 75 | // evaluated. |
---|
| 76 | // priority: Integer? |
---|
| 77 | // The order in which to exec this callback relative to other callbacks, defaults to 1000 |
---|
| 78 | // context: Object?|Function |
---|
| 79 | // The context in which to run execute callback, or a callback if not using context |
---|
| 80 | // callback: Function? |
---|
| 81 | // The function to execute. |
---|
| 82 | // |
---|
| 83 | // example: |
---|
| 84 | // Simple DOM and Modules ready syntax |
---|
| 85 | // | require(["dojo/ready"], function(ready){ |
---|
| 86 | // | ready(function(){ alert("Dom ready!"); }); |
---|
| 87 | // | }); |
---|
| 88 | // |
---|
| 89 | // example: |
---|
| 90 | // Using a priority |
---|
| 91 | // | require(["dojo/ready"], function(ready){ |
---|
| 92 | // | ready(2, function(){ alert("low priority ready!"); }) |
---|
| 93 | // | }); |
---|
| 94 | // |
---|
| 95 | // example: |
---|
| 96 | // Using context |
---|
| 97 | // | require(["dojo/ready"], function(ready){ |
---|
| 98 | // | ready(foo, function(){ |
---|
| 99 | // | // in here, this == foo |
---|
| 100 | // | }); |
---|
| 101 | // | }); |
---|
| 102 | // |
---|
| 103 | // example: |
---|
| 104 | // Using dojo/hitch style args: |
---|
| 105 | // | require(["dojo/ready"], function(ready){ |
---|
| 106 | // | var foo = { dojoReady: function(){ console.warn(this, "dojo dom and modules ready."); } }; |
---|
| 107 | // | ready(foo, "dojoReady"); |
---|
| 108 | // | }); |
---|
| 109 | |
---|
| 110 | var hitchArgs = lang._toArray(arguments); |
---|
| 111 | if(typeof priority != "number"){ |
---|
| 112 | callback = context; |
---|
| 113 | context = priority; |
---|
| 114 | priority = 1000; |
---|
| 115 | }else{ |
---|
| 116 | hitchArgs.shift(); |
---|
| 117 | } |
---|
| 118 | callback = callback ? |
---|
| 119 | lang.hitch.apply(dojo, hitchArgs) : |
---|
| 120 | function(){ |
---|
| 121 | context(); |
---|
| 122 | }; |
---|
| 123 | callback.priority = priority; |
---|
| 124 | for(var i = 0; i < loadQ.length && priority >= loadQ[i].priority; i++){} |
---|
| 125 | loadQ.splice(i, 0, callback); |
---|
| 126 | onEvent(); |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | has.add("dojo-config-addOnLoad", 1); |
---|
| 130 | if(has("dojo-config-addOnLoad")){ |
---|
| 131 | var dca = dojo.config.addOnLoad; |
---|
| 132 | if(dca){ |
---|
| 133 | ready[(lang.isArray(dca) ? "apply" : "call")](dojo, dca); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | if(has("dojo-sync-loader") && dojo.config.parseOnLoad && !dojo.isAsync){ |
---|
| 138 | ready(99, function(){ |
---|
| 139 | if(!dojo.parser){ |
---|
| 140 | dojo.deprecated("Add explicit require(['dojo/parser']);", "", "2.0"); |
---|
| 141 | require(["dojo/parser"]); |
---|
| 142 | } |
---|
| 143 | }); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | if(domReady){ |
---|
| 147 | domReady(handleDomReady); |
---|
| 148 | }else{ |
---|
| 149 | handleDomReady(); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | return ready; |
---|
| 153 | }); |
---|