2015-11-08 41 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