【问题标题】:Starting/Stopping Marionette Modules and Routing启动/停止 Marionette 模块和路由
【发布时间】:2013-07-15 15:09:49
【问题描述】:

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

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

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

【问题讨论】:

    标签: backbone.js marionette


    【解决方案1】:

    您在每条路线中启动和停止每个模块似乎有点矫枉过正。 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...
      })
    };
    

    【讨论】:

    • 这是一个绝妙的答案。也许,我正在考虑如何错误地处理我的模块。我的印象是这样的事情才有意义。毕竟,为什么要一个模块,也许,从 HomeApp 运行 3 次点击。如果这是最好的方法,我肯定会使用这种方法。是否有充分的理由不考虑仅在实际需要时才启动模块?
    • 根据我的经验,有选择地启动模块对几个不同的事情很有用。 1. 您有单独的脚本,这些脚本不属于您的主应用程序,但您在某些页面上会需要这些脚本。一个很好的例子是使用 d3.js 的图形库,您只需要该代码和应用程​​序特定部分的周围模块。 2. 你的模块负责大部分不相关的启动代码。例如,类似于一个现场演示页面,它会在启动时生成一堆假数据。
    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    相关资源
    最近更新 更多