2015-11-08 39 views
0

我有一個模型和視圖,將模型獲取數據之前在那裏視圖渲染後取。當我檢查數據庫時,當我在視圖中進行更改時,數值仍然保留在數據庫中。但在視圖渲染時,我無法通過model.fetch()獲取值。模型數據撕心裂肺的觀點

define([ 
    "marionette", 
    "monet", 
    "modules/fetchParams/collections/fetchParamsListLevels", 
    "modules/fetchParams/views/fetchParamsLayoutView", 
    "modules/fetchParams/models/fetchParam" 
], function(Marionette, Monet, fetchParamListLevels, FetchParamsLayoutView,FetchParamModel){ 
    return Marionette.Module.extend({ 
     model:"", 
     onStart:function(){ 
      this.model =new FetchParamModel(); 
     }, 
     displayInRegionMarketParams:function(region){ 
      this.fetchModel("market"); 
      region.show(new FetchParamsLayoutView({model: this.model, title: " Market"})); 
     }, 
     displayInRegionShareParams:function(region){ 
      this.fetchModel("shares"); 
      region.show(new FetchParamsLayoutView({model: this.model, title: "Shares"})); 
     }, 
. 
. 
. 
fetchModel:function(type){ 
      this.model.setType(type); 
      this.model.fetch(); 
     } 

我試過如下,但無法找到解決方案。讓我知道我在這裏失蹤。

this.listenTo(this.model, "change", this.render()); 

回答

0

看起來你不小心在你的this.render回調中加了括號。回調的一點是,它會被召喚你,所以你不必這樣做。所以你不用括號將它傳遞給事件監聽器,它會爲你運行它。

您的代碼會以這種方式工作:

this.listenTo(this.model, "change", this.render); 

此外,如果你想執行什麼剛過將數據從服務器來了,你可能會使用從fetch()方法返回的承諾:

this.model.fetch().done(function(data, status, xhr){ 
    console.log(data, status, xhr); 
    /** update the view maybe */ 
}); 
+0

感謝您的投入。我有一個關於懷疑model.fetch(),model.fetch後()完成,它被自動設定值模型。還有一件事我試圖把我的區域獲取的成功回調裏面,有問題的區域爲「區域爲未定義」 – CNKR

+0

是,'model.fetch()'自動新的數據集模型,這樣你就可以聽在我的答案的第一部分中使用'listenTo'進行更改。 – Yura

+0

同樣區域可能恰好是不確定的,因爲你失去的上下文,這意味着'this'是不是同一個對象實例了。 – Yura