Lightweight (1KB) dependency management built on jQuery Deferreds.
Dependencies
jQuery 1.5+
Getting Started
Include waiter.js anywhere after jQuery.
Waiter is built for modular applications. Module names associate a key to a payload, allowing you to delay execution of a function until dependencies are met. The payload can be anything: text, json, a function; often it will be a URL to a javascript file.
Before diving in, let's look at the two most important function signatures.
void
load
( Stringname
, Variedpayload
[, Objectoptions
]);
The load
method registers an object or URL, automatically loading it if necessary (via jQuery.ajax). The object or resource pointed to by the URL will be used to resolve the promise returned by waitFor
.
$.Promise
waitFor
( [ Arraynames
] [, Stringname*
] );
waitFor
accepts either an array of module names or a variable-length parameter list of names. These represent the dependencies that need to be loaded before the promise is resolved. Potential gotcha: notice that waitFor
accepts strings and returns a promise. First-time users may find themselves wrongly passing a function to waitFor
instead of attaching the function with .then()
.
Define and use a local module
In the example below, the function that depends on MyModule
will delay execution until it has loaded.
waiter; waiter;
Multiple modules
waiter;
Optionally pass an array instead of individual arguments:
var dependencies = 'MyModule1' 'MyModule2'; waiter;
Define and use a remote module (or data)
waiter; waiter;
Works with jsonp too
waiter;waiter;
Optionally specify expected export
waiter; waiter;
Note that remote modules, data, and so on are all cached in memory. If the remote is expected to be dynamic, unload the module before calling it again:
waiter;waiter;
TODO: Maybe a per module cache option?
Leverage $.Deferred callbacks
waiter
Middleware
waiter;
More examples
See our interactive examples file.
Configuration
Call the config()
method before using modules or at any time to overwrite values. See below for defaults:
waiter;