2013-11-28 43 views
0

我想构建一个使用烬数据的小应用程序,我试图通过它的相册呈现艺术家的所有歌曲。Ember.js烬数据呈现双重嵌套hasMany关系

我的模式是这样的:

App.Artist = DS.Model.extend({ 
    name: DS.attr('string'), 
    albums: DS.hasMany('album', {async:true}) 
}); 

App.Song = DS.Model.extend({ 
    title: DS.attr('string'), 
    artist: DS.belongsTo('App.Artist'), 
    album: DS.belongsTo('App.Album') 
}); 

App.Album = DS.Model.extend({ 
    title: DS.attr('string'), 
    cover_url: DS.attr('string'), 
    artist: DS.belongsTo('artist'), 
    songs: DS.hasMany('song', {async:true}) 
}); 

而且在模板我试图使它像:正确显示

<script type='text/x-handlebars', data-template-name='artists'> 
    {{#each artist in model}} 
    {{#linkTo 'artist' artist}}{{artist.name}}({{artist.albums.length}}){{/linkTo}} 
    {{/each}} 
    {{outlet}} 
</script> 

<script type='text/x-handlebars', data-template-name='albums'> 
     {{#each album in albums}} 
     <h3>{{album.title}}</h3> 
     {{#each song in album.songs}} 
      {{song.title}} 
     {{/each}} 
     {{/each}} 
</script> 

专辑标题,但歌曲标题不显示。 Ember向服务器发送请求,加载专辑和专辑的歌曲。歌曲是DS.ManyArray:ember461

的回应是这样的:

{ 
    songs: [ 
    { 
    id: 8 
    artist_id: 1, 
    album_id: 5, 
    title: "title" 
    } 
] 
} 

可能是什么album.songs的理由不beeing来拆分?

感谢您的帮助。

回答

1

的问题是,我指定宋的关系不正确:

App.Song = DS.Model.extend({ 
    title: DS.attr('string'), 
    artist: DS.belongsTo('App.Artist'), 
    album: DS.belongsTo('App.Album') 
}); 

变为:

App.Song = DS.Model.extend({ 
    title: DS.attr('string'), 
    artist: DS.belongsTo('artist'), 
    album: DS.belongsTo('album') 
}); 
0

除非你有一个特殊的串行器,或者Ember Data的老版本,否则你的json是错误的。

{ 
    songs: [ 
    { 
    id: 8 
    artist_id: 1, 
    album_id: 5, 
    title: "title" 
    } 
] 
} 

应该

{ 
    songs: [ 
    { 
    id: 8 
    artist: 1, 
    album: 5, 
    title: "title" 
    } 
] 
} 
+0

谢谢您的回答。我试图将其更改为您的格式,但它不能解决问题。 http://emberjs.com/guides/models/the-rest-adapter/#toc_relationships本文档是否过时? – Matthias

+0

更改为您的格式修复了一些其他问题,我喜欢album.artist.title之前没有工作 - 但它不能解决歌曲问题。 – Matthias