简体   繁体   English

Ember.js余烬数据呈现双重嵌套的hasMany关系

[英]Ember.js ember-data render double nested hasMany relationship

I am trying to build a little app using ember data and I am trying to render all songs of an artist through its albums. 我正在尝试使用余烬数据构建一个小应用程序,并试图通过其专辑渲染艺术家的所有歌曲。

My models look like: 我的模型如下:

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})
});

And in the template I am trying to render it like: 在模板中,我尝试将其呈现为:

<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>

The album title is displayed correctly but the song titles are not shown. 专辑标题正确显示,但歌曲标题未显示。 Ember sends a request to the server loading the songs for the albums and album.songs is DS.ManyArray:ember461 . Ember向服务器发送请求,以加载服务器上的专辑和专辑的歌曲。歌曲是DS.ManyArray:ember461

The response looks like: 响应如下所示:

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

What could be the reason for album.songs not beeing resovled? 专辑歌曲无法重新播放的原因可能是什么?

Thanks for your help. 谢谢你的帮助。

The problem was that I specified the relationships on Song incorrectly: 问题是我在Song上指定的关系不正确:

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

becomes: 变为:

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

Unless you have a special serializer, or an older version of Ember Data, your json is wrong. 除非您有特殊的序列化程序或旧版本的Ember Data,否则您的json是错误的。

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

should be 应该

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM