2015-10-07 60 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