骨干错误,对象[对象全局]没有方法......

时间:2013-07-04 10:16:51

标签: javascript backbone.js requirejs

在导航器下面的代码中抛出一个错误,我使用backbone.js和require.js从视图中加载书籍,这是我的代码:

BookListView.js

define([
    'jquery',
    'underscore',
    'backbone',
    'collections/bookCollection',
    'views/bookView'
], function ($, _, Backbone, bookCollection, bookView) {

    var books = [{ ... }];

    var BookListView = Backbone.View.extend({
        el: $('#books'),

        initialize: function () {
            _.bindAll(this, 'render', 'renderBook');
            this.collection = new bookCollection(books);
            this.render();
        },

        render: function () {
            _.each(this.collection.models, function (item) {
                this.renderBook(item);
            });
        },

        renderBook: function (item) {
            var BookView = new bookView({
                model: item
            });
            this.$el.append(BookView.render().el);
        }
    });

    return BookListView;

});

控制台返回错误:对象[object global]没有方法'renderBook'

你能帮帮我吗?

1 个答案:

答案 0 :(得分:2)

尝试为每次调用添加明确的上下文:

_.each(this.collection.models, function (item) {
    this.renderBook(item);
}, this);