2014-09-12 80 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 
});