ready.js 4.2 KB

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