Skip to content Skip to sidebar Skip to footer

Circular Dependencies For A Web App Using Backbone.marionette And Requirejs

I am in the following situation. I am using requireJs to loads module and I don't want to use global variables. The main.js is responsible to load the router. Then the router loads

Solution 1:

the schema:

 main.js -> router -> app -> subApp -> router

is right.

If you are using backbone.marionette, in order to access the router from the app and subApp, without using global var, you should start the app in router in this way:


// router.jsYourApp.start(router: router);

// app.jsYourApp.addInitializer(function(options){
  // do useful stuff herevar myView = newMyView({
    router: options.router
  });
  YourApp.mainRegion.show(myView);
});

Solution 2:

Subapp can raise events which router handles rather than having an explicit dependency on router

Solution 3:

In my project, I use the following dependency: main.js -> app -> router -> subApp.

In app.js, I create a single global variable that holds a pointer to my app:

define([...], function(...) {
return {
  initialize: function() {
    window.MyApp = newBackbone.Marionette.Application();
    // ...MyApp.start();
  }
};
});

This makes it extremely easy to access my app's regions from anywhere, as well as store global state information in one name space.

I tried doing it without the global app at first, but eventually gave up and found this approach to be a lot more flexible.

Post a Comment for "Circular Dependencies For A Web App Using Backbone.marionette And Requirejs"