Circular Dependencies for a web app using backbone.marionette and requireJs

后端 未结 3 1952
北恋
北恋 2021-02-15 13:49

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.

3条回答
  •  庸人自扰
    2021-02-15 14:31

    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 = new Backbone.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.

提交回复
热议问题