簡體   English   中英

骨干收集未觸發添加事件

[英]Backbone Collection Not firing Add Event

所以我有一個問題,即我有一個骨干集合,該集合用於創建將數據保存到REST API的功能。 數據被保存到服務器,模型被添加到當前集合,但是集合的add事件不會被觸發。下面是代碼片段:視圖初始化函數

intialize : function() {
        this.listenTo(this.collection, 'add', this.updateList);
    },      

updateList函數僅執行控制台日志。使用該集合保存數據的views函數為:

cards = this.collection;
        debugger
        cards.create(data, {
            success : function(model, response) {
                console.log("success on saving card");
                console.log(response);
                console.log("Updating list");
                console.log(cards);
            },
            error : function(model, response) {
                console.log("error on saving card");
                console.log(model);
                console.log("response");
                console.log(response);
            }
        })
        return false;

在您的視圖中嘗試以下代碼:

initialize: function() {
    this.collection.on('add', this.updateList, this);
}

要么:

var someCollection = new SomeCollection();
var view = new SomeView({collection: someCollection});
view.listenTo(someCollection, 'add', view.updateList);

您為什么不只使用: this.model.on('change', doAction, this); 在視圖內? 如果我理解正確,這是一個更好的解決方案,因為您的模型正在更改。 另外,我在任何地方都找不到在On()函數中強制使用ListenTo()的地方,您能告訴我您在哪里看到的嗎?

暫無
暫無

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

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