Backbone.js 基于Backbone.history.fragment的控制器文件动态加载
我正在构建一个有50多个模块的应用程序。使用require函数动态加载控制器+路由器是个好主意吗?下面是我在app.js文件中的sudo代码,我正在与require.js和木偶一起使用Backbone.js 基于Backbone.history.fragment的控制器文件动态加载,backbone.js,requirejs,marionette,Backbone.js,Requirejs,Marionette,我正在构建一个有50多个模块的应用程序。使用require函数动态加载控制器+路由器是个好主意吗?下面是我在app.js文件中的sudo代码,我正在与require.js和木偶一起使用 app.on("initialize:after", function(){ if(Backbone.history){ Backbone.history.start(); /* For now I am considering the app with have only single
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函数。我没有在代码中添加任何保护措施,当我了解这是否是一个好方法时,我会这样做。您的路由器应该包含控制器。而不是相反 由于您有50个模块,我建议您查看
marinotte.module
及其addInitializer()
和startWithParent
,以便在需要加载时引导所有这些模块
您可以使用requireJS和Marionette.module而不会出现问题