2

我们如何在不明确告诉路由的控制器方法启动/停止每个模块的情况下处理路由之间的启动/停止模块。

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
  }
}

我知道我在这里做错了,希望不是从根本上,但如果有更好的方法,请告诉我。模块管理将成为该项目的关键,因为它将主要在平板设备上运行。

4

1 回答 1

3

您在每条路线中启动和停止每个模块似乎有点矫枉过正。Marionette 中并没有太多内置功能可以帮助您处理这样的模块。

如果您真的想要这样做,我建议您为您的路由编写一个包装器,该包装器需要一个模块列表来启动,并且我会在启动/停止模块后运行。

像这样的东西:

(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...
  })
};
于 2013-07-15T18:10:13.527 回答