backbone.js - 启动/停止 Marionette 模块和路由

标签 backbone.js marionette

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

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

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

最佳答案

启动和停止每条 route 的每个模块似乎有点过分了。 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...
  })
};

关于backbone.js - 启动/停止 Marionette 模块和路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17657559/

相关文章:

javascript - 尽管尚未加载 dom,但如何在主干中声明 jquery 元素

javascript - 如何在成功回调函数(Backbone)中使用参数

javascript - require.js 和 r.js 依赖项

javascript - Backbone.marionette : jQuery document. 已准备好,未在路线上运行

javascript - 主干 View collection.each() 错误?

javascript - `bindAll` 过时了吗?

javascript - Marionette 中的僵尸景观

javascript - jQuery 重新渲染/绘画错误

javascript - 主干命名空间开放 View

javascript - 如何在不更改主干页面的情况下更改网址