2015-10-07 55 views
0

后,这里是计划收集的backbonejs应用部分:骨干模型取回收集的模型旧值将它们设置为新值

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 = 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函数wh它为集合中的每个模型设置日期属性。

它的工作原理。但由于某种原因,一段时间后,它又回到了旧的价值。我不明白为什么会发生。

我可以打开时间表视图后看到的值: http://pastebin.com/Jdv4tmHs

有没有其他的代码,故意改变后的值。我认为我的视角和模型的方式有问题。或者在其他地方有一个模型副本。你能帮忙吗?

EDIT 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; 

下有没有功能的内循环中它通过全系车型,并更改值回旧... 谁知道它为什么会发生?

回答

0

只是一个盲点,但也许:你设置模型,但你不保存它们。所以每次获取时都会获得旧值。

+0

我不需要保存它。但它只提取一次。正如您在提取后所看到的那样更改数据。主干是否由于某种原因(不是由用户发起的)提取数据? – maximus

+0

好问题,尝试修改模型以在数据更改时记录某些内容,以便您可以看到它何时发生(或者只是逐步调试) – HPeter

+0

一步一步没有帮助。它会根据某种原因进行更新,这可能是骨干网内的某个地方。而且在网络日志中只有一次获取请求。 – maximus