【问题标题】:Dynamic loading of controller files based on Backbone.history.fragment基于Backbone.history.fragment动态加载控制器文件
【发布时间】:2014-03-05 21:50:43
【问题描述】:

我正在构建一个包含 50 多个模块的应用程序。使用 require 函数动态加载控制器+路由器是个好主意吗?以下是我与 require.js 和 marionette 一起使用的 app.js 文件中的 sudo 代码

app.on("initialize:after", function(){
  if(Backbone.history){
    Backbone.history.start();

    /* For now I am considering the app with have only single 
    level routing, something like http://localhost#module and
    app will always call list method from controller */    

    var moduleName = Backbone.history.fragment;
    var controllerFile = "app/controller/" + moduleName + "controller";

    require([controllerFile], function() {
      app.trigger( moduleName + ':' + 'list');
    }
  }
})

我的控制器文件包含路由器,它会在初始化必要的模型后直接调用视图。我看到使用这种方法的唯一风险可能是在控制器文件完全加载之前调用 require 函数。我没有在代码中添加任何保护措施,当我了解这是否是一个好方法时,我会这样做。

【问题讨论】:

    标签: backbone.js requirejs marionette


    【解决方案1】:

    您的路由器应该包含控制器。反之亦然。

    由于您有 50 个模块,我建议您查看 Marionette.module 及其 addInitializer ()startWithParent,以便在需要加载所有这些模块时引导它们。

    您可以毫无问题地同时使用 requireJS 和 Marionette.module。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多