Skip to content

AMD Modules vs Marionette's Modules

Patrick Romowicz edited this page Aug 5, 2013 · 8 revisions

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.

Using Marionette's Modules With AMD

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.

Which Is Right For You?

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.

Clone this wiki locally