See definejs for an example implementation and usage
I'd like to request that the AMD 'define()' have the following signatures:
define(module value | module factory)
Defines an anonymous module, extracting its ID from the module's path. If a module factory is specified then the factory is called and the module's value will be equal to its return value.
define({/* properties of the module */});
define(function() {
return {
/* module properties */
};
});
define([dependency array of module IDs], module value | module factory)
Defines an anonymous module, extracting its ID from the module's path. If a module factory is specified then the factory is called with the values of each dependent module loaded from the dependency array, and the module's value will be equal to the factory's return value. The module factory will only be called after each dependency has loaded and the dependcies wil be passed to the factory in the same order as the dependency array.
define(['a', 'b'], {/* module properties */});
define(['a', 'b'], function(a, b) {
return {
/* module properties */
};
});
define(module ID, [dependency array of module IDs], module value | module factory)
Defines a named module, where the specified module ID is used for caching instead of the modules path. If a module factory is specified then the factory is called with the values of each dependent module loaded from the dependency array, and the module's value will be equal to the factory's return value. The module factory will only be called after each dependency has loaded and the dependcies wil be passed to the factory in the same order as the dependency array.
define('myModule', ['a', 'b'], {/* module properties */});
define('myModule', ['a', 'b'], function(a, b) {
return {
/* module properties */
};
});
define({imports}, module value | module factory)
[new signature]
Where 'imports' is an object where each key is the name of an import and whose value is a dependent module ID. Defines an anonymous module, extracting its ID from the module's path. If a module factory is specified then the factory is called with an object where each key from the imports object equals the corresponding loaded module. The module's value will be equal to the factory's return value. The module factory will only be called after each dependency has loaded.
define({a: 'a', b: 'b'}, {/* module properties */});
define({a: 'a', b: 'b'}, function(imports) {
return {
/* imports.a and imports.b are the module values from loading 'a' and 'b' */
/* module properties */
};
});
define(module ID, {imports}, module value | module factory)
[new signature]
Where 'imports' is an object where each key is the name of an import and whose value is a dependent module ID. Defines named module, where the specified module ID is used for caching instead of the modules path. If a module factory is specified then the factory is called with an object where each key from the imports object equals the corresponding loaded module. The module's value will be equal to the factory's return value. The module factory will only be called after each dependency has loaded.
define('myModule', {a: 'a', b: 'b'}, {/* module properties */});
define('myModule', {a: 'a', b: 'b'}, function(imports) {
return {
/* imports.a and imports.b are the module values from loading 'a' and 'b' */
/* module properties */
};
});
The following signatures are not so import to include, but offer an object-literal technique, so that the arguments are
are explicitly named and easier to understand when reading code.
define({module: module value | module factory})
define({id: module ID, module: module value | module factory})
define({imports: {imports}, module: module value | module factory})
define({id: module ID, imports: {imports}, module: module value | module factory})
NOTES:
At the very least I'd would love to see the addition of the import object. The advantage of using an import object is that the import name of each module being imported is given at the same location where the dependency is specified and not in the module factory signature. This permits a module factory to have one formal paramter for all imports which much easier to read for modules that have several dependencies.