不会再次比较ID添加的同型号

不会再次比较ID添加的同型号

本文介绍了骨干collection.fetch({更新:真正})不会再次比较ID添加的同型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与我的Backbone收集视图添加不会被调用与模型

我有我的模型

  VAR的客户= Backbone.Model.extend({
    idAttribute:IP
});
VAR殖民地= Backbone.Collection.extend({
    网址:'/ presence /敲',
    模式:客户端
});

我也试过

  VAR的客户= Backbone.Model.extend({
    ipAttribute:功能(){
        返回this.options.ip
    }
});

我还使用 ID试过属性,而不是使用 IP 。但没有任何工程。这似乎骨干,是比较对象。 不可以 idAttribute 因此,如果两个对象是完全同它不是触发添加但是如果有任何变化,这是不是在 idAttribute 仍认为该模型是新的和触发器添加

我有一个小提琴在使用模拟AJAX(基于上面提到的问题,我问几天前的溶液)。


解决方案

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添加的同型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:11