1

在下面的代码中,导航器向我抛出了一个错误,我正在使用带有 require.js 的主干.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 [object global] has no method 'renderBook'

请问你能帮帮我吗?

4

1 回答 1

2

尝试将上下文明确添加到每个调用中:

_.each(this.collection.models, function (item) {
    this.renderBook(item);
}, this);
于 2013-07-04T10:22:21.743 回答