未捕获的类型错误:对象#<对象>没有方法'绑定'-脊梁骨

Uncaught TypeError: Object #<Object> has no method 'bind' - backbone

本文关键字:对象 gt 有方法 脊梁骨 绑定 lt 类型 错误      更新时间:2024-04-29

我得到以下错误:带有主干的Uncaught TypeError: Object #<Object> has no method 'bind'

var UpdatingPostView = PostView.extend({
    initialize: function (options) {
        this.render = _.bind(this.render, this);
        this.model.bind('change:title', this.render);
    }
});

这是调用函数。

posts.fetch().complete(function() {
    var postCollectionView = new PostCollectionView({
        collection: posts,
        el: $('.posts')[0]
    });
    postCollectionView.render();
}); 

这是PostCollectionView

var PostCollectionView = Backbone.View.extend({
    initialize: function (post) {
        var that = this;
        this._postViews = [];
        this.collection.each(function() {
            that._postViews.push( new UpdatingPostView({
                model: post,
                tagName: 'div'
            })); 
        });
    },
    render: function () {
        var that = this;
        //Clear out this element
        $(this.el).empty();
        //Render each view and append to parent element
        _(this.postViews).each(function (dv) {
            $(that.el).append(dv.render().el);
        })     
    }

这里的post是什么?

initialize: function (post) {
    var that = this;
    this._postViews = [];
    this.collection.each(function() {
        that._postViews.push( new UpdatingPostView({
            model: post,

当您早些时候实例化它时,您使用。。。

var postCollectionView = new PostCollectionView({
    collection: posts,

我不是Backbone方面的专家,但我很确定它不会将一个单独的帖子传递给一个集合的初始化器,该集合会传递一个帖子数组。

我认为你的初始值设定项应该看起来像这样:

initialize: function () {
    var that = this;
    this._postViews = [];
    this.collection.each(function(post) { // <<<<< function(post) is important
        that._postViews.push( new UpdatingPostView({
            model: post,