Dojo.require() is your friend

One of the cool parts of Dojo is the ability to only load the modules and widgets your page needs. This is done with the dojo.require() method. Wherever your script is you can call dojo.require() and the module will be loaded in real time. The follow is an example of some script blocks that may be in the page header of an HTML page:

http://js/dojo.xd.js
 dojo.require("dojox.encoding.base64");
 dojo.require("dojo._base.xhr");
 dojo.require("dijit._base.manager");
 dojo.require("dijit.form.Button");
 dojo.require("dijit.Toolbar");
 dojo.require("dijit.Tooltip");
</script>

Dojo.require() does a lot of work for you. For instance it will not load the modules more than once and will load all dependent modules for you. One thing you will read in the help is the combination of using dojo.require() with dojo.addOnLoad(). This insures the module is loaded before you continue executing any relying code. So it is a best practice to chain these calls together when calling require() dynamically.

dojo.addOnLoad(function(){
  dojo.require("dijit.Dialog");
  dojo.require("dijit.TitlePane");
  dojo.addOnLoad(function(){
      // dijit.Dialog and friends are ready, create one from a node with id="bar"
      var dialog = new dijit.Dialog({ title:"Lazy Loaded" }, "bar");
  });
});
-link
Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.