[英]Backbone model getting back old values of models of collection after setting them to new values
這是骨架js應用程序中的Schedule集合的一部分:
App.Schedule = App.Games.extend({
/* url: 'data/json/server/schedules.json' */
url: '../rest/schedule',
initialize: function() {
var self = this;
this.fetch({
success: function() {
self.updateLocalTime();
app.eventBus.events.trigger('schedule:created', "schedule");
}
});
},
updateLocalTime: function() {
var timeUTC = "";
var timeLocal = "";
for (var ind = 0; ind < this.models.length; ind++) {
timeServer = this.models[ind].get('date');
timeLocal = convertServerTimeToLocal(timeServer);
this.models[ind].set('date', timeLocal.getTime());
}
},
....
}
游戲是:
App.Games = Backbone.Collection.extend({
model: App.Game,
url: '/api/games/'
});
App.Game是:
App.Game = Backbone.RelationalModel.extend({
urlRoot: 'data/json/game.txt',
idAttribute: 'id',
relations: [{
type: Backbone.HasOne,
key: 'team1',
relatedModel: 'App.GameTeam',
reverseRelation: {
key: 'game1',
includeInJSON: 'id'
}
}, {
type: Backbone.HasOne,
key: 'team2',
relatedModel: 'App.GameTeam',
reverseRelation: {
key: 'game2',
includeInJSON: 'id'
}
}
]
});
如您所見,在成功獲取之后,它將調用updateLocalTime函數,該函數將date屬性設置為集合中的每個模型。
而且有效。 但是一段時間后,由於某種原因,它會恢復為舊值。 我不明白為什么會這樣。
打開時間表視圖后,我可以看到這些值: http : //pastebin.com/Jdv4tmHs
沒有其他代碼有意地將值改回。 我認為視圖和模型的處理方式存在問題。 或者在其他地方有模型的副本。 您能幫忙嗎?
編輯1:
ScheduleView加載到以下代碼中之前的時間表:
app.myTeam.schedule = new App.Schedule();
編輯2:簡單來說,scheduleView是:
var ScheduleView = Backbone.View.extend({
initialize: function() {
...
this.mySchedule = new MyScheduleView({
el: "#schedule_myschedule_view"
});
...
this.render();
}, ...
}
var MyScheduleView = ScheduleView.extend({
initialize: function() {
...
this.collection = app.myTeam.schedule;
...
},
}
編輯3:
我找到了更改收藏模型的地方。 我使用骨干關系和骨干。 在其內部的代碼中,它已更改。
這是發生在骨干Relationaljs內部的地方:
/**
* Override Backbone.Collection.set, so we'll create objects from attributes where required,
* and update the existing models. Also, trigger 'relational:add'.
*/
var set = Backbone.Collection.prototype.__set = Backbone.Collection.prototype.set;
在循環中該函數的內部,它遍歷所有模型並將值更改回舊模型...誰知道為什么會發生?
只是一個盲目的鏡頭,但也許是:您設置了模型但沒有保存它們。 因此,每次獲取時,它都會獲得舊值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.