2013-07-31 68 views
1

我有RestAdapter一個應用程序,從服務器將適當的數據:如何在顯示視圖之前使Ember.js加載RESTAdapter數據?

App.AFile= DS.Model.extend({ 
    name: DS.attr('string'), 
... 

App.Store = DS.Store.extend({ 
    revision: 13, 
    adapter: DS.RESTAdapter.extend({url: "myapi"}) 
}); 

和地圖是這樣的:

App.Router.map(function() { 
    this.resource('allfiles', { path: '/allfiles' }); 
    this.resource('onefile', {path: '/onefile/:onefile_id' }); 

和路線定義是這樣的:

App.allfilesRoute = Ember.Route.extend({ 
    model: function() 
    { 
     return App.AFile.find(); 
    } 
}); 

App.onefileRoute = Ember.Route.extend({ 
    model : function(params) 
    { 
     return App.AFile.find(params.onefile_id); 
    } 
}); 

而那些模板:

<script type="text/x-handlebars" data-template-name="allfiles"> 
{{#each controller}} 
    {{#linkTo onefile this}}open{{/linkTo}} 
{{/each}} 
</script> 

<script type="text/x-handlebars" data-template-name="onefile"> 
    {{name}} 
</script> 

它的工作方式如下:用戶打開應用程序,並使用名爲open的鏈接顯示所有文件模板。鏈接打開一個名爲onefile的模板並將onefile_id傳遞給它。

當我打開應用程序並單擊打開它的作品並顯示一個文件的正確名稱。 URL設置爲#/ onefile/1,其中1是onefile_id。所以它工作正常。

但是,當我刷新頁面(#/ onefile/1)比名稱不再顯示。

我已經檢查了發生了什麼,並在onefileRoute模型函數之前,我返回id,它發生App.AFile具有空值定義的所有字段。在應用程序加載後,這些值在App.AFile對象中正確填充,但不會顯示在視圖中。

因此,看起來RestAdapter在視圖顯示後獲取數據。

如何使它工作?

回答

0
App.onefileRoute = Ember.Route.extend({ 
    model : function(params) 
    { 
     var m = App.AFile.find(params.onefile_id); 
     // at this point m might not be resolved, so name could be empty 
     return m; 
    }, 
    afterModel: function(model, transition){ 
     // at this point it should be resolved, what happens here? 
     console.log(model.get('name')); 
    } 
}); 

我的猜測是你的端點正在通過id返回模型的錯誤信息。

+0

在aftermodel model.get('name')仍然返回空 –

+0

在Chrome中,如果您觀看網絡選項卡,您是否看到呼叫和響應? – Kingpin2k

相關問題