-
Notifications
You must be signed in to change notification settings - Fork 0
AMD Modules vs Marionette's Modules
Marionette provides it's own very simple module system, through the use of the .module
method on Application instances.
MyApp = new Marionette.Application();
MyApp.module("MyModule", function(MyModule, MyApp, Backbone, Marionette, $, _){
// do stuff here ...
// see the full API Documentation for more info
});
The purpose of this module definition is to provide a simple alternative to RequireJS / AMD modules, but Marionette's modules also provide one benefit that AMD doesn't directly - the ability to start and stop modules.
To get the benefit of Marionette's modules within AMD modules, you don't need to use the additional closure. You can get a module definition directly from the .module(...)
call, and add initializers and finalizers directly:
define([
"MyApp",
"Marionette"
], function(MyApp, Marionette){
var MyModule = MyApp.module("MyModule");
var SomeView = Marionette.ItemView.extend({
// ...
});
MyModule.addInitializer(function(){
MyModule.someView = new SomeView();
MyApp.someRegion.show(MyModule.someView);
});
MyModule.addFinalizer(function(){
MyApp.someRegion.close();
});
return MyModule;
});
You can use this directly inside of an AMD module to provide support for starting and stopping your AMD module code.
If you're not using AMD / RequireJS, or another module definition system, Marionette's built in modules may be a good option for you. But if you're already using AMD / RequireJS, or another module system, then you should stick with that.