Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#13113 closed enhancement (fixed)

refactor DOMContentLoaded and require.ready APIs into separate modules

Reported by: Rawld Gill Owned by: Rawld Gill
Priority: high Milestone: 1.7
Component: Loader Version: 1.6.1
Keywords: Cc:
Blocked By: Blocking:

Description

This machinery has traditionally been part of the bootstrap (dojo) or loader (RequireJS, bdLoad). A major factor instructing this decision was the require/dojo.ready function that needed to detect both DOMContentLoaded and the internal loader state that all requested modules had been loaded. Unfortunately, this design has the significant drawbacks.

  1. Due to browser inconsistencies, the DOMContentLoaded detection machinery is often the subject of disagreement. There are multiple discussion threads on the dojo-interest, AMD-implement, and requireJS lists that touch on this subject in some way. By containing this machinery in the loader, it is more-difficult to replace with an alternate implementation.
  1. For some types of apps, trivial detection of window.onload is sufficient. Containing the complete DOMContentLoaded detection in the loader is inconvenient.
  1. Foreign loader implementations of this machinery are variable (there is no specification). Consequently, dojo must provide an external module to implement the machinery for compat with foreign loaders and maintaining the machinery in both the loader and a module is redundant.

For all these reasons, both the domReady and ready detection machinery should be factored into separate modules and removed from the loader.

Change History (10)

comment:1 Changed 8 years ago by Rawld Gill

Resolution: fixed
Status: newclosed

(In [25266]) moves DOMContentLoaded etc. from loader to modules; adds dojo/domReady (thanks Bryan Forbes); renames dojo/_base/load to dojo/ready; see #13113 for details; fixes #13113; !strict

comment:2 Changed 8 years ago by Rawld Gill

Further details on [25266]

dojo.ready/addOnLoad work as always. They have been moved from the module dojo/_base/load to dojo/ready; dojo/ready is included as part of dojo/main in the browser.

The build-in loader DOMContentLoaded machinery has been moved to the module dojo/domReady. This module is courtesy of Bryan Forbes; thanks Bryan! To execute upon DOMContentLoaded, write

require(["domReady!"], function(){
  // can operate on DOM here
});

require.ready has been removed. Instead of...

require.ready(function(){
 // stuff
});

...write...

require(["dojo/ready"], function(ready){
  ready(function(){
    // stuff
  });
});

Several tests used the syntax sugar provided by the dojo loader:

require.ready([<deps>], <callback>);

Since require.ready was removed, this syntax sugar was removed. In all cases, it appears that checking for the DOMContentLoaded condition (compared to the ready condition) was sufficient. Therefore, these expressions have been replaced with...

require([<deps>, "domReady!"], <callback>);

The loader has a tiny event API that replaces a couple of methods: require.on(type, listener):

  • require.onConfig(cb) is now require.on("config", cb)
  • require.onError(cb) is now require.on("error", cb)
  • we now have require.on("idle", cb) which signals the loader is idle (all requested modules have been loaded)

As per dojo guidelines, the result of require.on can be disconnected by calling remove(). E.g.

// listen for the first idle, disconnect, and do something
var listener= require.on("idle", function(){
  listener.remove();
  // do something
});

Lastly, the loader provides the method require.idle() which returns true if the loader is idle (all requested modules have been loaded) and false otherwise.

comment:3 Changed 8 years ago by Rawld Gill

Since require.ready has been removed; the RequireJS config switch "ready" has also been removed.

Similalrly, the property require.pageLoaded has been removed.

Use the dojo config switch addOnLoad as instead.

comment:4 Changed 8 years ago by Rawld Gill

(In [25268]) removed debuggin noise; refs #13113; !strict

comment:5 Changed 8 years ago by Rawld Gill

(In [25274]) rewrote require.ready calls to use dojo/domReady plugin ; refs #13113

comment:6 Changed 8 years ago by Rawld Gill

(In [25275]) rewrote require.ready calls to use dojo/domReady plugin ; refs #13113

comment:7 Changed 8 years ago by Chris Mitchell

(In [25285]) refs #13113 fix mobileGallery in async mode \!strict

comment:8 Changed 8 years ago by Rawld Gill

(In [25467]) moved dojo.ready calls in base to dojo/ready module; added feature to load parser as per #12907; fixes #12907; refs #13113

comment:9 Changed 8 years ago by Rawld Gill

(In [25545]) improved doc load complete detection to simulate document.readyState for old browsers; refs #13113; !strict

comment:10 Changed 8 years ago by Kris Zyp

In [26408]:

Don't silently swallow errors, refs #13113 !strict

Note: See TracTickets for help on using tickets.