Skip to content
jrburke edited this page Aug 15, 2011 · 13 revisions

The require function in an AMD implementation differs from the traditional CommonJS require. There is a strong need for a callback-based require, since dynamically computed dependencies may load asynchronously.

API Specification

Local vs Global require

The local require is the require function passed to an AMD define factory function. Examples:

    define(['require'], function (require) {
        //the require in here is a local require.
    });

    define(function (require, exports, module) {
        //the require in here is a local require.
    });

There MAY be a global require function available, and if so, it MUST support the same API as the local require. The global require MAY support require.config(), but the local require MUST NOT implement require.config().

Both local and global require MAY support other APIs specific to the implementation.

require(String)

Synchronously returns the module export for the module ID represented by the String argument. Based on the CommonJS Modules 1.1.1 require.

Example:

    define(function (require) {
        var a = require('a');
    });

require(Array, Function)

The Array is an array of String module IDs. The modules that are represented by the module IDs should be retrieved and once all the modules for those module IDs are available, the Function callback is called, passing the modules in the same order as the their IDs in the Array argument.

Example:

    define(function (require) {
        require(['a', 'b'], function (a, b) {
            //modules a and b are not available for use.
        });
    });

require.toUrl(String)

Converts a String that is of the form [module ID] + '.extension' to an URL path. require.toUrl resolves the module ID part of the string using its normal module ID-to-path resolution rules, except it does not resolve to a path with a ".js" extension. Then, the '.extension' part is then added to that resolved path. Example:

    //cart.js contents:
    define (function(require) {
        //module ID part is './tmeplates/a'
        //'.extension is '.html'
        //templatePath may end up to be something like
        //'modules/cart/templates/a.html'
        var templatePath = require('./templates/a.html');

    });

require.config(Object)

This API MUST NOT be implemented for local require. It only applies to a global require.

Configures the implementation with information from the Object passed to require.config(). require.config() MAY return a require function that obeys the configuration. Example:

    var configuredRequire = require.config({
        baseUrl: 'scripts'
    });

    //If an implementation only supports one environment for require
    // or has a default environment for require, the developer could just
    //use require() instead of configuredRequire here.
    configuredRequire(['a', 'b'], function (a, b) {
        //a and be are now loaded according to the configuration
        //passed to require.config()
    });