2
我在我的Collection中有一個事件監聽器,它監聽'add'事件,當我在路由器中添加的示例數據添加時,似乎只會觸發'add'事件,但不會在我在視圖中添加更多的車型,兩者的代碼如下:Backbone Collection'add'event not firing
收藏:
Application.Collection.extend({
name: "todos/todos",
initialize: function() {
this.on("add", console.log("added"));
},
url: "/todo-application/todos"
});
路由器(此觸發事件):
collection = new Application.Collections["todos/todos"]();
var m = new Application.Model({
title: 'Finish screencast',
done: true
});
collection.add(m);
視圖(這不火一個事件):
Application.View.extend({
name: "todos/index",
events: {
"submit form": function(event) {
event.preventDefault();
var attrs = this.serialize();
this.collection.add(attrs);
this.$('input[name=title]').val('');
}
}
});
視圖中的collection.add
肯定工程,作爲模型被添加到集合,並顯示在屏幕上,但在添加樣本數據集合時只給了我一個console.log()
初始頁面加載.. 。
編輯:
根據我的骨幹Chrome擴展事件確實是發射,但由於某些原因,這個事件綁定不工作,我已經使用this.listenTo
代替.on
試過。
你收集在通過施工時風景?例如'新視圖({collection:collection})' – ashley