[英]Backbone Collection Not firing Add Event
So i have a problem whereby I have a backbone collection that am using to create function to save data to a REST API. 所以我有一个问题,即我有一个骨干集合,该集合用于创建将数据保存到REST API的功能。 The data is saved to the server and a model is added to the current collection but then the add event for the collection is not fired.Below are snippets of the code The views initialize function
数据被保存到服务器,模型被添加到当前集合,但是集合的add事件不会被触发。下面是代码片段:视图初始化函数
intialize : function() {
this.listenTo(this.collection, 'add', this.updateList);
},
The updateList function only does a console log.The views function that saves data using the collection is: 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;
Try this code in your view: 在您的视图中尝试以下代码:
initialize: function() {
this.collection.on('add', this.updateList, this);
}
Or: 要么:
var someCollection = new SomeCollection();
var view = new SomeView({collection: someCollection});
view.listenTo(someCollection, 'add', view.updateList);
why don't you just use: this.model.on('change', doAction, this);
您为什么不只使用:
this.model.on('change', doAction, this);
inside the view? 在视图内? if I understood correctly, this is a better solution since your model is changing.
如果我理解正确,这是一个更好的解决方案,因为您的模型正在更改。 plus, I couldn't find anywhere a place where the ListenTo() is mandatory over the On() function, can you tell me where you saw it?
另外,我在任何地方都找不到在On()函数中强制使用ListenTo()的地方,您能告诉我您在哪里看到的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.