对象渲染在Backbone中没有方法调用

时间:2014-02-17 00:06:00

标签: javascript backbone.js

我在模型中有这个方法 -

        getDisplayedData: function(){
        data = this.get('data')
        displayedData = this.get('displayedData')
        title = this.get('title')
        newData = [];
        dataToShow = this.get('dataToShow');
        _.each(dataToShow, function(value,key,list){
            nData = data.length;
            for(i = 0; i < nData ; i++){
                row = data[i];
                if(row[title] == value){
                    newData.push(row);
                }
            }
        });
        this.set("displayedData", newData);

        return newData;
    }

这就是它被称为的方式 -

        this.collection.each(function(model,key,list){
        data = model.getDisplayedData();
    });

我收到此错误 -

Uncaught TypeError: Object render has no method 'call' 

我知道这种情况正在发生,因为它实际上是一个集合对象,而不是DataModel。如何绑定它以使其引用模型?

在我的模型的init方法中,我这样做 -

    initialize: function(){
        console.log("INIT MODEL");
        console.log(this);
        return this;
    },

这就是打印 -

    r {cid: "c2", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
_changing: true
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
changed: Object
cid: "c2"
collection: r
getDisplayedData: function () { [native code] }
__proto__: s

这里没有设置 - 这就是问题(我认为)。

1 个答案:

答案 0 :(得分:0)

试试这个:

this.collection.models.each(function(model,key,list){
    data = model.getDisplayedData();
});