amdr.js is a small and very fast AMD-compliant asynchronous loader.
Size: less than 11KB (>5KB gzipped) using UglifyJS.
- Loads CommonJS AMD-formatted javascript modules in parallel.
- Loads CommonJS Modules (wrapped in a
define()
) - Loads non-AMD javascript files in parallel, too.
- Supports IE6+, FireFox, Chrome, Safari 3.2+, Opera 9.5+
- Tested with IE6+, FireFox 2+, Chrome 10+, Safari 4+, Opera 9.5+
define([name String], [dependencies Array], factory Function)
require(dependencies Array|String[, callback Function[, fallback Function]])
require.config([config Object])
config.debug
- boolean of amdr.js debug state (default isfalse
)config.timeout
- number of loading timeout in second (default is7
)config.urlBase
- string of base path (ex. '/path/to/base')config.urlArgs
- string of request parameter(s) to be appended (ex. 'key1=value1&key2=value2')config.urlExt
- string of default file extension (default is'.js'
)config.pathMap
- object of path mappings for module names not found directly under baseUrl. The path settings are assumed to be relative to baseUrl, unless the paths setting starts with a "/" or has a URL protocol in it ("like http:").config.shimMap
- object of shim mappings for traditional module.config.rewrite(name, parent)
- function to rewrite the module name.
require.config({
timeout: 15,
urlBase: '/another/path',
pathMap: {
'foo': '../bar/'
}
});
require(['foo/module', 'my/module', 'a.js', 'b.js'],
function(fooModule, myModule) {
//This function will be called when all the dependencies
//listed above are loaded. Note that this function could
//be called before the page is loaded.
//This callback is optional.
}
);
exports.load(request Object, name String, parent String, parents Object)
- This function dose module resource loading, returns a promise or a value.
request.config
- module configrequest.load([name String])
- uses default way to load a sub-modulerequest.toUrl(name String, config Config)
- convert name to url by configname
- request module nameparent
- parent module nameparents
- parent module names
export.rewrite(name String, parent String)
- optional- This function does dependency name rewriting, returns a string.
name
- request module nameparent
- parent module name
export.normalize(name String, normalize Function)
- optional- This function does module name normalizing, returns a string.
name
- name need to be convertednormalize(name String)
- default normalizer
require(path String, [callback Function], [fallback Function])
require other module under current module's contextexports
module exports objectmodule
module objectmodule.config
- module config objectmodule.exports
- object same as theexports
modulemodule.facorty
- function of module factorymodule.toUrl(name String, [config Object])
- converts a module name to the url by current module's context or the given config
Use require
module inside of a module to load a dynamic module.
This may help to solve some circular dependency issues.
new Deferred([executor(resolve, reject)])
is a class, implements Promise/A or Promise/A+.
A deferred represents an operation whose resolution is pending.
It has separate promise
and resolver
parts that can be safely given out to separate groups of consumers and producers, respectively, to allow safe, one-way communication.
deferred.promise
a Promise instance (implemented Promise/A or Promise/A+).
deferred.resolve([value *])
resolve promise.deferred.reject([reason *])
reject promise.deferred.state()
returns "padding", "resolved" or "rejected"deferred.then([callback Function], [fallback Function])
.callback
to be called with the value afterpromise
is fulfilled, orfallback
to be called with the rejection reason afterpromise
is rejected.
Deferred.resolve(promiseOrValue *)
returns the promise itself or a resolved promise of the value.Deferred.reject([reason *])
returns a rejected promise of the reason.
This is the css file loader.
- Usage:
require(['css!path/of/style.css']);
- Export:
<link>
element of thestyle.css
.
This is the conditional module loader.
- Usage:
require(['has!condition?module-a:module-b']);
- Export: condition matched module exports.
- API:
has(feature String) Boolean
has.add(feature String, definition Boolean|Function)
-
1.3.2
- supports module factory returns a promise.
- removed
Promise
built-in module. - Improved
Deferred
built-in module.- supports
new Deferred([executor(resolve, reject)])
- added
Deferred.resolve()
- added
Deferred.reject()
- supports
-
1.3.1
- bug fix.
-
1.3.0
- rewrites core functions.
- supports dependency name rewriting by plugin.
- separates module defining and executing.
- simplifies the internal plugin logic.
- simplifies
Promise
andDeferred
.- removed
promise.always()
function. - removed
deferred.notify()
function. - removed
progback
parameter ofpromise.then()
. - removed
progback
parameter ofdeferred.then()
.
- removed
- removed
config.config
property (require.config()
). - removed
hot
loader module.
-
1.2.x
- supports module shim.
- bug fix.
-
1.1.x
- exposes module dependencies, factory, parent and status.
- supports global module name rewriting.
- supports WebWorker.
- bug fix.
-
1.0.x
- basic functionality.
This project is released under MIT license.