Reputation: 396
How can we handle starting/stopping modules between routes without explicitly telling the route's controller method to start/stop each module.
var AppRouterController = {
index: function() {
// Start only modules I need for this route here
// in this case, HomeApp only
App.module('HomeApp').start();
// Stop all modules that should not be running for this route
// The idea, being that not everyone has to come to the index route first
// They could have been to many other routes with many different modules starting at each route before here
App.module('Module1').stop();
App.module('ModuleInfinity').stop();
// ...
// ...
// This could get tedious, expensive, and there has to be a better way.
},
someOtherRouteMethod: function() {
// Do it all over again
}
}
I know I am doing something wrong here, hopefully not fundamentally, but please let me know if there is a better way. Module management is going to be key for this project due to that fact that it will be running primarily on tablet devices.
Upvotes: 2
Views: 1882
Reputation: 9426
It seems like overkill that you are starting and stopping every module in each route. There isn't much built into Marionette to help you with juggling your modules like that.
What I would recommend if you really want to do that is to write a wrapper for your routes that takes a list of modules to start and I function to run after starting/stopping modules.
Something like this:
(function (App) {
var listOfAllModules = ["HomeApp", "Module1", ...];
window.moduleWrapper = function (neededModules, route) {
return function () {
_.each(_.without(listOfAllModules, neededModules), function (moduleName) {
App.module(moduleName).stop();
});
_.each(neededModules, function (moduleName) {
App.module(moduleName).start();
});
route.apply(this, arguments);
}
};
})(App);
Then, in your routers just wrap routes that need to juggle modules.
var AppRouterController = {
index: moduleWrapper(["HomeApp"], function() {
// Only routing logic left...
})
};
Upvotes: 3