Backbone收集空问题

时间:2016-04-19 10:16:36

标签: backbone.js

fetch: function() { 
        var self = this;
        self.each(function(track) {
            if(track) {
                track.destroy();
            }
        });
        Backbone.sync('fetch', this, {
            method: 'GET',
            success: function(response) {
                if (response) {
                    if(response.clips.length > 0) {
                        SpinnerOn();
                        for (i = 0; i < response.clips.length; i++) {
                            self.loadcount++;
                            self.loadFile(response.clips[i]);
                        }
                    }
                }
            }
        });
    }

在上面的fetch中,我试图清空集合,但它没有清空集合中的第一条记录,上面是同步操作我从数据库加载已保存的数据。

1 个答案:

答案 0 :(得分:1)

来自@ try-catch-finally用户评论我正在写这个,100%这将解决。

fetch: function() { 
    var self = this;
    while(self.length) { self.at(0).destroy() }
    Backbone.sync('fetch', this, {
        method: 'GET',
        success: function(response) {
            if (response) {
                if(response.clips.length > 0) {
                    SpinnerOn();
                    for (i = 0; i < response.clips.length; i++) {
                        self.loadcount++;
                        self.loadFile(response.clips[i]);
                    }
                }
            }
        }
    });
}

while(self.length){self.at(0).destroy()}这会破坏集合中的所有模型

相关问题