我有一个“业务”收集,我通过几个意见。
当我运行这个.business.fetch()时,返回的模型是完全相同的,但是事件'add'和'remove'被触发了,因为集合认为返回的模型是新的,所以它删除了'old'并添加了'new'模型。我能看到的模型之间的唯一区别是cid。
为什么取物者认为集合已经改变了?即使它没有改变?
这是我的代码:
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;
}
。
我有一个名为items的不同集合,它可以很好地提取。但由于某种原因,当我运行business.fetch()时,在第一次提取时,它会添加两个businesscid 19 & 20,然后在第二次提取时,它会删除cid 19 & 20并添加cid 23 & 24,尽管它们是相同的
我在控制台中看到以下内容:
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…}
.属性“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]
。
。
这给我带来了一个问题,因为我的navbar render()在每次onBusinessAdd函数被调用时都会被调用,并且由于某种原因,jasny-bootstrap navbar会在每个render()上出错并克隆自己,这会导致延迟。(我必须在修复这个问题后再研究这个问题)
1条答案
按热度按时间chy5wohz1#
您的响应似乎没有
id
属性,而是具有_id
属性。要么你应该发送它是
id
,要么你应该通知 Backbone 网使用_id
来跟踪模型使用idAttribute
选项。官方文档中的示例也有一个
_id
: