2013-03-22 58 views
0

我正在做一个新的需求/骨干应用程序的初始设置,我似乎无法让我的“全局资源”对象进行通信一些子模块。我怀疑它与一些循环依赖有关,但我不确定在哪里/为什么/如何。Backbone.js&Require.js - 应用程序组件无法与路由器通信

毕竟在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”,尽管在最初的开球被定义,是不是我的全局命名空间的一部分设置断点看,因此不能被控制器使用。我错过了什么吗?我没有正确定义这个吗?

回答

0

你没有包括您mainRouter.js的最后一部分... 一个愚蠢的问题:你在末尾添加

return MainRouter; 

+0

我确实返回了MainRouter,是的。 – 2013-03-25 13:33:22

相关问题