0

backbone.jsに問題があります。すべてのコードは機能しますが、例外が発生します TypeError: 'undefined' is not an object (evaluating '(e=this.models[c]).cid')

例外は、モデルの数が制限を超えた場合に発生し、コレクションでself.remove()を呼び出します。

var Column = Backbone.Collection.extend({
    initialize: function(col) {
        var view = new ColumnView({
            id: "column_" + col.id,
            column: col
        });

        view.render();
    },

    comparator: function(tweet) {
        return tweet.get('created_at_time');
    },

    clean: function() {
        var self        = this;
        var total       = this.models.length;
        var threshold   = (total - window.config.threshold) - 1;

        if(threshold > 0) {
            console.log("Removing if tweet is older then " + threshold);
            this.models.forEach(function(tweet, index) {
                if(index < threshold) {
                    self.remove(tweet);
                }
            });
        }
    }
});

誰かが何が起こっているのか知っていますか?エラーはサファリで発生します。

4

1 に答える 1

1

推測では、モデルのリストを繰り返し処理しているときにモデルを削除することが原因です。

試す

if (threshold > 0) {

    var removed = [];
    this.models.forEach(function (tweet, index) {
        if (index < threshold) {
            removed.push(tweet);
        }
    });
    this.remove(removed);
}

または@muによって提案されたバリアント

if (threshold > 0) {

    var removed =  this.filter(function(model, index) {
            return index < threshold;
    });
    this.remove(removed);
}

またはあなたの場合はもっと簡単です

if (threshold > 0) {
    this.remove(this.models.slice(0, threshold));
}
于 2012-08-13T14:07:53.583 に答える