marionettejs/backbone.marionette — pull request #792 by andru (2013-11-23 10:44:26+00:00)
Title: Added asynchronous module initializers.

This pull request adds asynchronous module initializers using jQuery Deferred objects.

It adds three new properties to Marionette.Module:
* _deferReady
* ready
* _initializerPromises
_deferReady is a jQuery Deferred object intended for internal use by Marionette.Module.
ready is a jQuery Promise object which will be resolved or rejected based on the result of any defined asynchronous initializers. It is intended for use as a "public" property.
_initializerPromises is a Marionette.Promises instance (see below)

It adds one event and corresponding triggerMethod:
* ready
The ready event will be triggered when the ready promise has been resolved.

It adds one new method:
* addAsyncInitializer
This adds an asynchronous initializer. The function should return a promise which will be resolved or rejected when complete. If it returns any other value it will be marked as resolved, but in that case a standard initializer should be used anyway.

It adds one new Marionette object
* Marionette.Promises
This is modelled very closely on Marionette.Callbacks, and indeed there may be some way to combine their functionality, but I didn't want to mess with such a core dependency. Essentially it's the same, but the run method adds the return value of the callback to an array, and returns a new promise. When all items in the array are resolved, the promise will be marked as resolved.

This pull request allows application code (other modules, routers, etc) to wait until all asynchronous module dependencies have resolved before continuing. Eg.
`MyModule.ready().done(function(){ /*do something*/ });`
jQuery.when can be used to define multiple dependencies
Eg.
`$.when(FirstModule, SecondModule).done(function(){ /* do something */ });