未捕获的TypeError:Object#<object>没有方法'bind' - backbone </object>

时间:2013-12-06 13:47:13

标签: javascript backbone.js

我收到以下错误: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);
        })     
    }

1 个答案:

答案 0 :(得分:1)

这里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,