简体   繁体   English

骨干获取每次都删除并读取相同的模型

[英]Backbone fetch removes and readds the same models every time

I have a 'business' collection which I pass to several views. 我有一个“业务”收藏集,并传递了几种看法。

When I run this.business.fetch(), the models returned are exactly the same but the events 'add' and 'remove' get triggered because the collection thinks the models returned are new so it removes the 'old' and adds the 'new' models. 当我运行this.business.fetch()时,返回的模型完全相同,但是触发了事件“添加”和“删除”,因为集合认为返回的模型是新的,因此删除了“旧”并添加了“新”模型。 The only difference i can see between the models is the cid. 我能看到的模型之间的唯一区别是cid。

Why does the fetch think the collection has changed? 为什么提取指令认为集合已更改? even though it hasn't? 即使没有?

This is my code: 这是我的代码:

router.js
var Router = Backbone.Router.extend({
routes: {
    "home": "home",
}
initialize: function() {
  this.business = new Business();
  this.business.fetch();
},
home: function() {
  var homepageView = new HomePageView({business: this.business});
},

.

HomePageView.js
var HomePageView = Backbone.View.extend({
initialize: function (options) {
  this.business = options.business;
  this.listenTo(this.business, "add",    this.onBusinessAdd);
  this.listenTo(this.business, "remove", this.onBusinessRemove);
  this.listenTo(this.business, "change", this.onBusinessRemove);
  this.business.fetch();
},
onBusinessAdd: function(model) {
  console.log(model);
},
onBusinessRemove() & onBusinessChange() are the same as onBusinessAdd()

.

collection/business.js
var BusinessCollection = BaseCollection.extend({
initialize : function() {
  this.urlRoot = this.path + 'business/me';
  this.url = this.path + 'business/me';
  this.model = BusinessModel;
}

.

I have a different collection called items, and that fetches perfectly. 我有一个不同的集合,称为项,并且可以很好地获取。 But for some reason, when i run business.fetch() on the first fetch, it adds 2 businesses cid 19 & 20, then on the second fetch, it deletes cid 19 & 20 and adds cid 23 & 24 even though they are the same 但是由于某种原因,当我在第一次读取时运行business.fetch()时,它将添加两个cid 19和20业务,然后在第二次读取时,它将删除cid 19和20并添加cid 23和24,即使它们是相同

i get the following in my console: 我在控制台中得到以下内容:

first fetch: (router.js)
"onBusinessAdd"
n {cid: "c19", attributes: Object, collection: n, _changing: false, _previousAttributes: Object…}
n {cid: "c20", attributes: Object, collection: n, _changing: false, _previousAttributes: Object…}

second fetch: (homepageview.js)
"onBusinessRemove"
n {cid: "c19", attributes: Object, collection: n, _changing: false, _previousAttributes: Object…}
n {cid: "c20", attributes: Object, collection: n, _changing: false, _previousAttributes: Object…}
"onBusinessAdd"
n {cid: "c23", attributes: Object, collection: n, _changing: false, _previousAttributes: Object…}
n {cid: "c24", attributes: Object, collection: n, _changing: false, _previousAttributes: Object…}

. The attributes of "c19" == "c23" and "c20" == "c24" “ c19” ==“ c23”和“ c20” ==“ c24”的属性

Object {_id: "567b6997a58029fc087b1716", hasLogo: false, approved: false, postcode: "2131", suburb: "1231321"…}
__v : 0
_id : "567b6997a58029fc087b1716"
approved : false
created : 1458794097462
email : "f@f.com"
hasLogo : false
name : "afsaf"
phone : "0123456789"
postcode : "2131"
reservationlist : Array[1]
settingsSchema : Array[1]
staff : Array[1]
street : "123132"
suburb : "1231321"
waitlist : Array[1]

.

.

this causes a problem for me as my navbar render() gets called every time the onBusinessAdd function gets called and for some reason the jasny-bootstrap navbar bugs out and clones itself on every render() which causes lag. 这给我造成了一个问题,因为每次调用onBusinessAdd函数时都会调用我的navbar render(),并且由于某种原因,jasny-bootstrap navbar会出错并在每个render()上克隆自身,这会导致延迟。 (i'll have to look into that after i fix this problem first) (我必须先解决此问题后再调查)

Your response doesn't seem to have an id attribute , but has an _id property instead. 您的响应似乎没有id属性 ,但是有_id属性。

either you should sent it is id , or you should inform backbone to use _id for tracking models using the idAttribute option. 您应该将其发送给id ,或者应该使用idAttribute选项通知骨干网使用_id跟踪模型。

The example in official docs has an _id as well: 官方文档中的示例也具有_id

var Meal = Backbone.Model.extend({
  idAttribute: "_id"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM