我有一個模型,其中包含集合。在我的Web應用程序中,我有一個項目模型,並且我有一個名爲「協作者」的屬性,這個屬性實際上是一組用戶。骨幹嵌套集合模型偵聽添加
在我看來,我有以下的初始化,
this.model.get('collaborators').on('change', alert("!!"), this);
當視圖首創的負載,我得到一個警告,firther到視圖我有一個事件,將觸發以下,
var newModel = new app.User({ id: this.clickedElement.data('userId') });
console.log(this.model.get(this.formSection)); // shows that the collection has 4 models
newModel.fetch({
success: function() {
that.model.get(that.formSection).add(newModel);
console.log(that.model.get(that.formSection)); //shows that the collection has 5 models
}
});
正如你在上面的代碼中看到的那樣,我正在記錄我的合作者集合,它顯示了長度爲4,在成功方法中獲取後,我是否添加了剛剛提取到該集合的模型,然後記錄收藏再次,這次它返回的長度爲5.
這對我來說意味着添加成功,那麼「添加」事件偵聽器在初始頁面視圖之後不會觸發嗎?
我沒有看到一個'add'監聽器。只有一個'change' - 但那是你的合作者收集。集合不會觸發'change'事件,我不確定它是否會冒泡到父模型。 – Lix 2014-08-28 09:48:04
即使我做'this.model.get('collaborators')。on('add',alert(「!!」),this);'它只會觸發視圖的初始加載,而不會在向集合中添加更多數據時使用。 – Udders 2014-08-28 09:53:13
您是否嘗試過僅在父模型中使用'change'事件? – Lix 2014-08-28 09:57:43