0

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?

maximus
  • 4,201
  • 15
  • 64
  • 117

1 Answers1

0

Just a blind shot, but maybe: you set the model but you do not save them. So every time it fetches, it gets the old value.

HPeter
  • 39
  • 7
  • I don't need to save it. But it fetches only once. As you see after fetching it changes data. Does backbone fetches data by some reason (not initiated by user)? – maximus Oct 07 '15 at 09:42
  • Good question, try to modify the model to log something out when the data is changed, so you can see when it happens ( or just debug step by step ) – HPeter Oct 07 '15 at 10:38
  • step by step doesn't helps. It does update by some reason which is probably somewhere inside backbone. moreover the fetching request happens only once in the network logs. – maximus Oct 07 '15 at 10:47