骨干collection.fetch({更新:真正})不会再次比较ID添加的同型号 [英] Backbone collection.fetch({update: true}) doesn't compare id add's same model again
问题描述
在与我的<一的另一个问题关系href=\"http://stackoverflow.com/questions/18964354/backbone-collection-view-add-not-being-called-with-a-model\">Backbone收集视图添加不会被调用与模型
我有我的模型
VAR的客户= Backbone.Model.extend({
idAttribute:IP
});
VAR殖民地= Backbone.Collection.extend({
网址:'/ presence /敲',
模式:客户端
});
我也试过
VAR的客户= Backbone.Model.extend({
ipAttribute:功能(){
返回this.options.ip
}
});
我还使用 ID试过
属性,而不是使用 IP
。但没有任何工程。这似乎骨干,是比较全对象。 不可以的 idAttribute
因此,如果两个对象是完全
同它不是触发添加
但是如果有任何变化,这是不是在 idAttribute
仍认为该模型是新的和触发器添加
我有一个小提琴在 http://jsfiddle.net/3QE3u/
使用模拟AJAX(基于上面提到的问题,我问几天前的溶液)。
如果有这不是在idAttribute任何改变仍然认为
该模型是新添加触发器
块引用>在在拨弄你的code您还绑定集合的变事件的addAll功能
this.collection.bind('变化',this.addAll);
如果你对此有何评论这条线,你的code将作为你的预期。
这是如何工作现在:
当第一个读取功能称为添加事件被触发用于IP127.0.0.2和变革的项目被激发到IP127.0.0.1的项目。当获取函数被调用第二次变事件被触发两个项目。对于每个连续的调用来获取,变事件被触发了两个项目。 (边注:刷新事件永远不会触发的话)您可以编写另一种方法来处理变事件,在该方法中你可以更新现有的HTML元素,以反映最新情况。
In relation with My another question on Backbone collection View add not being called with a model
I've my models
var Client = Backbone.Model.extend({ idAttribute: 'ip' }); var Colony = Backbone.Collection.extend({ url: '/presence/knock', model: Client });
I've also tried
var Client = Backbone.Model.extend({ ipAttribute: function(){ return this.options.ip } });
I've also tried using the
id
attribute instead of usingip
. But nothing works. It seems Backbone is comparing the whole object. not theidAttribute
So If two objects areEXACTLY
same its not triggeringadd
However If there is any change which is not inidAttribute
is still thinks the model is new and triggersadd
I've a Fiddle on
http://jsfiddle.net/3QE3u/
that uses mock ajax (based on the solution of the above mentioned question that I asked few days ago).解决方案If there is any change which is not in idAttribute is still thinks the model is new and triggers add
In your code in the fiddle you have also binded "change" event of the collection to "addAll" function
this.collection.bind('change', this.addAll);
If you comment this line, your code will work as you expected.
Here it how it works now: When first fetch function is called "add" event is fired for the item with ip "127.0.0.2" and "change" is fired for the item with ip "127.0.0.1". When fetch function is called for the second time "change" event is fired for both items. For each consecutive call to fetch, "change" event is fired for two items. (side note: refresh event is never trigger at all)
You can write another method to handle the "change" event and in that method you can update existing html element to reflect the update.
这篇关于骨干collection.fetch({更新:真正})不会再次比较ID添加的同型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!