0

我正在写一个简单的例子来学习 Backbone 和 Django-Tastypie。我正在使用 Backbone.js 1.0。

在控制台中,当我尝试时notes = new NoteListView();出现错误TypeError: Cannot call method 'bind' of undefined

从我在网上阅读的内容来看,我认为这是因为模板没有实例化。但是,模板缓存在视图上的模板调用中。所以,我很困惑。

也许我的想法是完全错误的,但应该发生的是绑定发生在已缓存的模板上,以便它可以进行设置并填充模板。

TastpieCollection 和 TasttypieModel 来自http://paltman.com/2012/04/30/integration-backbonejs-tastypie/这似乎工作得很好。

$(function() {

    // Note: The model and collection are extended from TastypieModel and TastypieCollection
    // to handle the parsing and URLs

    window.Note = TastypieModel.extend({});

    window.Notes = TastypieCollection.extend({
        model: Note,
        url: NOTES_API_URL
    });

    window.NoteView = Backbone.View.extend({

        template: _.template($('#notes-item').html()),

        initialize: function () {
            _.bindAll(this, 'render');
            this.model.bind('set', this.render);
        },
        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    })

    window.NoteListView = Backbone.View.extend({

        template: _.template($('#notes-item-list').html()),

        initialize: function () {
            _.bindAll(this, 'render');
            this.collection.bind('set', this.render);
        },
        render: function() {
            var $notes,
                collection = this.collection;
            this.$el.html(this.template());
            $notes = this.$('.notes');
            collection.each(function(note){
                var view = new NoteView({
                    model: note,
                    collection: collection
                });
                $notes.append(view.render().el);
            });
            return this;
        }
    })
})

所以,两个问题:

问题一:错误是什么,我该如何解决?

问题二:set版本是怎么reset用的……对吗?

谢谢你的帮助。

4

1 回答 1

0

您没有设置集合,这就是您的错误消息的原因。您应该执行以下操作来修复该特定错误消息:

notes = new NoteListView({collection : new Notes()});
于 2013-10-17T22:21:13.210 回答