Backbone.js& Require.js - App Components无法与Router通信

时间:2013-03-22 22:17:06

标签: backbone.js namespaces requirejs

我正在对一个新的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”尽管在初始启动时定义,但它不是我的全局命名空间的一部分,因此控制器无法使用它。我错过了什么吗?我没有正确定义吗?

1 个答案:

答案 0 :(得分:0)

您没有包含主Router.is的最后一部分... 一个愚蠢的问题:你添加了

return MainRouter;

最后?