无法阅读房产' home'未定义的

时间:2015-07-14 01:48:22

标签: backbone.js requirejs

我对backbone.js和requirejs很新,我试图找出以下情况。

当我在" context.onScriptLoad"中加载我的应用程序时我的路由器的初始化功能被调用。我在这里使用我的app控制器,但它仍未定义。所以,我收到的错误是:

Cannot read property 'home' of undefined

我的路由器定义为:

define(['jquery', 'underscore', 'backbone', 'components/appController'],

    function ($, _, Backbone, appController) {

        var Router = Backbone.Router.extend({
            routes: {
                '': 'home',
                'details/:id': 'details',
                'createAgent': 'createAgent'
            },

            initialize: function () {

                var routeName;

                for (var r in this.routes) {
                    routeName = this.routes[r];

                    this.route(r, routeName, $.proxy(appController[routeName], appController));
                }
            },

            start: function () {
                Backbone.history.start();
            }

        });

        return new Router();
    }
);

似乎appController未初始化。

控制器定义为:

define(['jquery', 'underscore', 'backbone'],
    function ($, _, Backbone) {
        var appController = {
            currentView: null,

            home: function () {
                var self = this;
                require(['views/homeView'], function (HomeView) {
                    var View = new HomeView();
                    self.renderView.call(self, View);
                });
            },

            details: function (id) {
                var self = this;
                require(['views/detailsView'], function (DetailsView) {
                    var Agent = app.Agents.get(id);

                    var View = new DetailsView({ model: Agent });
                    self.renderView.call(self, View);
                });
            },

            createAgent: function () {
                var self = this;

                require(['views/createView'], function (CreateView) {
                    var View = new CreateView();
                    self.renderView.call(self, View);
                });
            },

            renderView: function (View) {
                this.currentView && this.currentView.remove();

                $('#main').html(View.render().el);
                this.currentView = View;
            }
        }
    });

我的app.js是

requirejs.config({

    baseUrl: 'scripts/libs',

    paths: {
        models: '../models',
        collections: '../collections',
        views: '../views',
        routers: '../routers',
        components: '../components',
        modalDialog: 'backbone.ModalDialog'
    },

    shim: {
        'backbone': {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone'
        },

        'underscore': { exports: '_' }
    }
})


var app = app || {};

require(['routers/router', 'components/dataService'], function (Router, dataService) {
    $(document).ready(function () {
        dataService.getData();

        Router.start();
    });
});

有人可以解释我如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你的控制器必须"返回AppController;"