Created
February 15, 2014 19:45
-
-
Save jasonLaster/9024318 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['jquery', 'backbone', 'marionette'], | |
function($, Backbone, Marionette){ | |
"use strict"; | |
var MarionetteModule = Marionette.Module; | |
//create new Marionette constructor | |
Marionette.Module = function(name, app){ | |
this.name = name; | |
this.vent = new Backbone.Wreqr.EventAggregator(); | |
//by default, do not start module with the application | |
this.startWithParent = false; | |
MarionetteModule.apply(this, arguments); | |
/* This is an imperfect solution, but without a rewrite of Marionette.Module it'll have | |
to do... | |
Marionette.Module currently provides no way of checking for the existence of a module definition | |
via a module name string without automatically creating it if it does not exist... | |
So, for now dependencies can only be siblings (that is, other modules defined as submodules of the this.app | |
object... dependency checking doesn't function for nested modules until I can find a way to check | |
for their existence) | |
*/ | |
this.dependencies = []; | |
} | |
//copy over all static methods and properties | |
_.extend(Marionette.Module, MarionetteModule); | |
//copy over the old Marionette.Module prototype | |
_.extend(Marionette.Module.prototype, MarionetteModule.prototype); | |
_.extend(Marionette.Module.prototype, { | |
constructor: Marionette.Module, | |
//create a convenience reference to the old start method | |
_start: Marionette.Module.prototype.start, | |
start: function(){ | |
//an array of promises returned by module dependencies | |
var dependencyPromises = []; | |
//start module dependencies first | |
if( this.dependencies.length ){ | |
_( this.dependencies ).each( function( moduleName ){ | |
//only start it if it's already defined and hasn't already been initialized | |
if(this.app.submodules[moduleName] && !this.app.submodules[moduleName]._isInitialized){ | |
this.app.log('Module %o is a dependency of module %o but is not initialized. Lazy initializing...', moduleName, this.name); | |
//start the dependency | |
this.app.submodules[moduleName].start(); | |
//add the module promise to the stack | |
dependencyPromises.push( this.app.submodules[moduleName].done ); | |
} | |
}, this); | |
//when all module dependencies are resolved, start this module | |
$.when.apply( this, dependencyPromises ) | |
.done( function(){ | |
this.app.log('All module dependencies loaded for %o. Starting.', this.name); | |
this._start.apply(this, arguments); | |
}.bind(this)) | |
.fail( function(){ | |
this.app.error('Module initialization failed.'); | |
}.bind(this)) | |
; | |
}else{ | |
//no dependencies, so just start the module | |
this._start.apply(this, arguments); | |
} | |
}, | |
isDependentOn: function(moduleName){ | |
this.dependencies.push(moduleName); | |
} | |
}); | |
return Marionette.Module; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment