Here is the part of Schedule collection in backbonejs application:
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());
}
},
....
}
The Games is:
App.Games = Backbone.Collection.extend({
model: App.Game,
url: '/api/games/'
});
App.Game is:
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'
}
}
]
});
As you see after successful fetch it calls updateLocalTime function which sets date attribute to each model in the collection.
And it works. But by some reason after some time it's set back to old values. I cant understand why it happens.
I can see the values after opening the Schedule View: http://pastebin.com/Jdv4tmHs
There is no other code that intentionally changes values back. I think there is something wrong with the way I did Views and Models. Or there is a copy of models in some other place. Could you please help with it?
EDIT 1:
the schedule before ScheduleView is loaded in code like this:
app.myTeam.schedule = new App.Schedule();
EDIT 2: Briefly the scheduleView is:
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;
...
},
}
EDIT 3:
I found the place where my collection models are changed. Im using backbone relational together with backbone. And somewhere inside of it's code its changed.
Here is the place inside of the backbone relationaljs where it happens:
/**
* 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;
Down there inside of that function in the loop it goes through all models and changes values back to old ones... Who knows why it happens?