Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the app and the app loads several subApps.

After everything has been initialised, the subApps needs the router for making router.navigate.

Here the schema:

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

Then I have a problem of Circular Dependencies and for that reason the router in subApp will be undefined.

What is the best way to reorganise my code or to fix this problem? Are there some example about this?

like image 968
Lorraine Bernard Avatar asked Jun 29 '12 16:06

Lorraine Bernard


2 Answers

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.js
YourApp.start(router: router);

// app.js
YourApp.addInitializer(function(options){
  // do useful stuff here
  var myView = new MyView({
    router: options.router
  });
  YourApp.mainRegion.show(myView);
});
like image 181
antonjs Avatar answered Sep 22 '22 00:09

antonjs


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

like image 28
Robert Levy Avatar answered Sep 23 '22 00:09

Robert Levy