2014-09-12 83 views
0

這是我遇到的一個常見問題,所以我想這是我做錯了。Ember無法鏈接相關模型

我有型號:

App.Post = DS.Model.extend({ 
    author: DS.hasMany('author') 
}); 

App.Author = DS.Model.extend({ 
    name: DS.attr('string'), 
    post: DS.belongsTo('post) 
}); 

單個REST調用來獲取一個崗位包括作者,所以我知道store.find('post')後,我在店裏所有文章和作者。然而,當我使用這個數據渲染頁面時(使用ArrayController,我注意到相關作者需要一些時間渲染,儘管只有一個請求。進一步的檢查顯示,在路由的setupController中,數據全部被加載到商店,但關係尚未確定如果我這樣做:。

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
     return this.store.find('post'); 
    }, 
    setupController: function(controller, model) { 
     model.forEach(function(post) { 
      console.log(post.get('author')); 
     }); 
     controller.set('model', model); 
    } 
}); 

控制檯日誌顯示了每個作者的「不確定」名單

只有一個請求到服務器的和我已經檢查過響應是否有效,並且最終呈現數據,但是幾秒鐘後undefined顯示出模板中的{{author.name}}。它看起來像數據被加載,但連接直到很晚才做出。在解釋這個文檔的文檔中我找不到任何東西,並且我無法想出一種方法來強制它在渲染之前正確加載。我已經使用loading模板,我試圖迫使它在model加載:

model: function() { 
    return Ember.RSVP.hash({ 
     posts: this.store.find('post'), 
     authors: this.store.find('author') 
    }); 
} 

但沒有什麼差別。

回答

0

你有沒有試過這個,因爲型號是承諾

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
     return this.store.find('post'); 
    }, 
    setupController: function(controller, model) { 
     model.then(function(result){ 
      result.forEach(function(post) { 
       console.log(post.get('author.name')); 
      }); 
     });   
     controller.set('model', model); 
    } 
}); 

,我不知道這是否是複製粘貼錯字或沒有,但值得檢查

App.Author = DS.Model.extend({ 
    name: DS.attr('string'), 
    post: DS.belongsTo('post) // missing the closing quote 
});