簡體   English   中英

骨干collection.reset添加一個不正確的模型

[英]Backbone collection.reset adds one incorrect model

我有一個通過搜索框過濾的集合。 當執行搜索/過濾時,我得到的結果是一個集合。 該集合是正確的,並且包含搜索中期望的模型。 當我使用collection.reset(result)它添加了一個主干集合,該主干集合包含一個與結果集合無關的模型,除了標准集合主干東西之外,它不包含任何其他內容。

集合

var Products = Backbone.Collection.extend({
    model: Product,
    url : '',

    search : function(letters){

        if(letters == "") return this;

        var pattern = new RegExp(letters,"gi");
        return new Products((this.filter(function(data) {
            return pattern.test(data.get("title"));
        })));
    }

});

從視圖:

    search : function (ev){
        var results = products.search($("#search").val());
        console.log("result");
        console.log(results);
        this.collection.reset(results);
        console.log("altered collection");
        console.log(this.collection);
    }

還有一張圖像顯示了它在記錄的不同點所包含的內容: 在此處輸入圖片說明

同樣,經過過濾的集合(結果之后)是100%正確的,並且我希望它是正確的。 this.collection事先重置的狀態也是正確的。 我唯一要做的就是this.collection.reset(result); 並再次記錄this.collection。 我聽了'reset'事件,當它觸發時,顯然沒有要渲染的模型,並且出現錯誤。

看起來results是“產品”集合。 collection.reset()需要一個模型數組,而不是一個集合。

試試this.collection.reset(results.models);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM