Backbone.js 启动/停止木偶模块和布线
我们如何在不明确告知路由控制器方法启动/停止每个模块的情况下处理路由之间的启动/停止模块Backbone.js 启动/停止木偶模块和布线,backbone.js,marionette,Backbone.js,Marionette,我们如何在不明确告知路由控制器方法启动/停止每个模块的情况下处理路由之间的启动/停止模块 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 shou
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函数 大概是这样的:
(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);
然后,在您的路由器中,只需包装需要处理模块的路由
var AppRouterController = {
index: moduleWrapper(["HomeApp"], function() {
// Only routing logic left...
})
};
这是一个奇妙的答案。也许,我正在考虑如何错误地处理我的模块。我觉得像这样的事情才有意义。毕竟,为什么一个用户想要一个模块,也许是从HomeApp上点击3下。如果这是最好的方法,我肯定会使用这种方法。有没有一个很好的理由不考虑启动模块只是因为它们实际上是需要的?在我的经验中,有选择地启动模块对于一些不同的东西是有用的。1.您有单独的脚本,它们不是主应用程序的一部分,但在某些页面上需要。一个很好的例子是一个使用d3.js的图形库,其中您只需要该代码和应用程序特定部分上的周围模块。2.您的模块负责大部分与其他方面无关的启动代码。例如,一个实时演示页面在开始时生成一堆虚假数据。