ready.js 4.5 KB

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