主干集合未触发添加事件
Backbone Collection Not firing Add Event
所以我有一个问题,我有一个主干集合,用于创建函数以将数据保存到 REST API。数据将保存到服务器,并将模型添加到当前集合,但随后不会触发集合的 add 事件。以下是代码片段视图初始化函数
intialize : function() {
this.listenTo(this.collection, 'add', this.updateList);
},
updateList 函数仅执行控制台日志。使用集合保存数据的视图函数为:
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(),你能告诉我你在哪里看到它吗?
相关文章:
- 如何在for循环中添加事件侦听器
- 如果选中了多个复选框,如何添加事件
- 正在ng重复元素上添加事件
- 在动态创建的标记上添加事件
- 如何在HTML列表中添加事件's子弹
- 将事件侦听器添加到文档,而不是签入元素存在,然后添加事件侦听器
- jQuery完整日历添加事件,仅包含月份和日期
- 是否可以在数字输入框中的小按钮中添加事件侦听器
- 在Google Chrome扩展中添加事件侦听器
- AngularJS:在.post()之后添加事件侦听器
- 对象 Javascript 中的标签无效 - 想要添加事件列表器
- 添加事件侦听器不起作用
- 如何为angular js条形图添加事件
- 在 http 请求中添加事件侦听器 ( request.on ) 是什么意思?而笏就是它的用途
- Iron:路由器在更改路由器上添加事件
- 如何为表行结果的每个按钮添加事件侦听器
- 在 javascript 中添加事件处理程序的位置
- YouTube API-动态添加事件侦听器
- 在聚合物的子菜单标题中添加事件
- 检查是否可以添加事件侦听器而不添加