I have been trying to figure out the best way to obtain my Marionette application's instance throughout my app. I am using requirejs, however I cannot figure out if it is possible to grab the actual instance of the app through require.
I have used Backbone.Wreqr.EventAggregator and callbacks in order to grab the app instance however that feels extremely messy.
I would also like to avoid passing the app instance through the constructors of my various views as well, as I feel there should be a better way.
This is at the bottom of my data-main file:
define('MyApp', ['App'], function (App) {
return new App();
});
require(['MyApp'], function (app) {
app.start();
Backbone.history.start({
pushState: true
});
});
This code works fine and boots up the app, however in my sub views and other components the app ends up being undefined. For example app is undefined in this scenario:
define([
'marionette',
'../controllers/Controller',
'MyApp'
], function (Marionette, Controller, app) {
var controller = new Controller();
var Router = new Marionette.AppRouter({
controller: controller,
appRoutes: {
'home' : 'showUserHome'
}
});
return Router;
});