我正在对一个新的 Require/Backbone 应用程序进行初始设置,但我似乎无法让我的“全局资源”对象与某些子模块进行通信。我怀疑它与一些循环依赖有关,但我不确定在哪里/为什么/如何。
在configure.js中加载所有填充项后,应用程序将启动:
/* kick off */
define([
'cxn/newUI/rr',
'cxn/newUI/routers/mainRouter'
], function( app, MainRouter) {
// create router
app.router = new MainRouter({
model: app
});
Backbone.history.start({
pushState: true,
root: app.rootURL
});
});
rr.js是我的“资源注册表”,其中定义了所有全局应用程序的东西
define(['underscore', 'backbone'], function(_, Backbone) {
// main namespace object
var ResourceRegistry = {
rootURL: '/',
models: {},
views: {},
collections: {},
router: {},
eventBus: _.extend( {}, Backbone.Events ),
tplCache: {}
};
return ResourceRegistry;
});
我的mainRouter.js启动,完成路由准备,并加载主控制器:
define([
'cxn/newUI/rr',
'cxn/newUI/controllers/_mainAppController',
'cxn/newUI/controllers/homeController',
'cxn/newUI/controllers/inboxController'
], function( app, MainAppController, HomeController, InboxController ) {
function cleanup() {
if (currentPage && currentPage.destroy) {
currentPage.destroy();
}
}
var currentPage;
var MainRouter = Backbone.Router.extend({
routes: {
'' : 'index',
'home' : 'home',
'messages' : 'showInbox',
'messages/:id' : 'showMessage',
'patient/:id' : 'showPatient'
},
initialize: function(options) {
this.model = options.model;
this.eventBus = app.eventBus;
this._listenForEvents();
},
// route definition methods
index: function() {
MainAppController.initialize({ eventBus: app.eventBus });
return this;
},
....
最后,我的主控制器加载并执行一个“postInitialize”钩子,一切都戛然而止。
define([
'cxn/newUI/rr',
'cxn/newUI/controllers/_baseController',
'cxn/newUI/views/_mainAppView/pageHeaderView',
'cxn/newUI/views/_mainAppView/mainContentView',
'cxn/newUI/views/_mainAppView/pageFooterView',
'cxn/newUI/views/_mainAppView/pageBottomDrawerView'
], function( app, Controller, PageHeaderView, MainContentView, PageFooterView, PageBottomDrawerView ) {
// Define the "new" collections/models/etc to pass to the controller here
// Define the controller
var MainAppController = new Controller({
name: '_mainAppController',
app: app,
postInitialize: function() {
app.eventBus.on('mainContentArea:loaded', function(route) {
if (!route) {
return app.router.navigate('home');
}
else {
return app.router.navigate(route);
}
});
//this._setupDocumentReady();
},
我得到错误Uncaught TypeError: Object #<Object> has no method 'navigate'
通过在 Chrome 开发工具中设置断点,我可以看到“app.router”尽管在初始启动中定义,但不是我的全局命名空间的一部分,因此不能被控制器使用。我错过了什么吗?我没有正确定义这个吗?