Three special module identifiers in require, module, and exports dojo

Source: Internet
Author: User

In the course of viewing the dojo source code, it was found that the three module names did not start with Dojo, Dijit, Dojox, and belonged to the special module name in the Dojo loader.

  

  

  Require

  This is a context-smart loader.

We have configured a Package:myapp with Dojoconfig, and now the files under the MYAPP directory can be requested in the following way:

This is "myapp/toplevelhandlers" define (["Dojo"], function (Dojo) {  dojo.connect (Dojo.byid ("Debugbutton"), " Click ", function () {    require ([" myapp/perspectives/debug "], function (perspective) {      perspective.open ();    });  });});

The file under a MyApp package is requested by the global require function to still use the form "package name + file path". If we want to load the module with a relative path like define: Reqiure (['./perspectives/debug '], function () {debug}), and require is unable to find "./perspectives/ Debug "This module's. This is the time for the context-smart require module to play.

This is "myapp/toplevelhandlers" define (["Dojo", "require"], function (dojo, require) {  dojo.connect (Dojo.byid (" Debugbutton ")," click ", Function () {    require (["./perspectives/debug "], function (perspective) {      Perspective.open ();});  

By referencing the Reqire module instead of the global require function, we can use a relative path to load the module, which is context intelligence.

Exports

Exports the function of this module is similar to that of exports in node. js, which is used to export methods or variables of the current module. In node it is the only export exported, but in dojo we can return an object literal directly in the module.

define ([], function () {  return {    someproperty: "Hello",    someotherproperty: "World"  };}); define (["exports"], function (exports) {  Exports.someproperty = "Hello";  Exports.someotherproperty = "World";});

In general, both of these are the same, but in the case of cyclic dependencies, the loader using exports dojo can determine whether the module is loaded, not the deadlock state.

Module

  

The module refers to modules that return an object that has three parameters:
    • ID: A unique module ID that uniquely identifies a module in the program; require ([Module.id], function (m) {}); M represents the module for the ID, which is often used in some functions that need to be stitched together by the module ID and a counter, such as the callback parameter in Dojo/request/script

    • URI: The absolute URL path that represents the module
    • Exports: This property is similar to the exports module above. The slight difference is that we can make exports point to a new object (usually a function) by this property. Similar to node in changing the exports reference in the module.
Define ("MyModule", ["module"], function (module) {  module.exports = Dojo.declare (/*...*/);});

The original exports property is an object and is now a class. The next time the module is referenced, it can be used to instantiate the object.

Define (["MyModule"], function (mymodule) {  var m = new MyModule ();});

  

These three special module is used when Dojo is used to be compatible with other Commonjs class libraries. Integrating dojo into node, for example, is another article.

Three special module identifiers in require, module, and exports dojo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.