Ember不渲染嵌入数据

时间:2013-05-01 21:34:27

标签: ember.js ember-data

我设置了一个可以使用Fixture Adapter运行良好的应用程序。

这是我用来渲染的模板:

  <div class="large-7 columns" id="song-list"> 
    <div class="playlist-meta row">
      <div class="large-12 columns">    
        <h4 class="subheader">Playlist</h4>    
        <h1 class="playlist-title">{{controller.name}}</h1> 
      </div>
    </div>

    {{#view App.SortableView}}  
    {{#each song in controller.songs}}
      <li class="row song">
        <button {{action "playSong" song}} class="toggle-play"><span class="entypo play"></span></button>
        <h4 class="subheader song-artist"><a href="#">{{song.artist}}</a></h3>
        <h3 class="song-title"><a href="#">{{song.name}}</a></h3>
      </li>    
    {{/each}}
    {{/view}}
  </div>

但是,当我使用REST适配器时,歌曲不会呈现。播放列表名称本身仍然呈现,但不是歌曲。

这是根据JSON返回给我的:

{"playlist":{"id":1,"name":"playlist","songs":[{"id":1,"name":"Song Name","artist":"Song Artist","soundcloud":"/tracks/1"}]}}

我使用Fixture数据时定义的模型:

App.Playlist = DS.Model.extend({
  name: DS.attr('string'),
  songs: DS.hasMany('App.Song'),
});

App.Song = DS.Model.extend({
  name: DS.attr('string'),
  artist: DS.attr('string'),
  soundcloud: DS.attr('string'),
  playlist: DS.hasMany('App.Playlist'),
});

App.Playlist.FIXTURES = [
  {
    id: 1,
    name: 'Playlist Name',
    songs: [1, 2, 3],
  }];
App.Song.FIXTURES = [
  {
    id: 1,
    name: 'Song Name',
    artist: 'Song Artist',
    soundcloud: '/tracks/12345',
  }];

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。我不知道这是不是'Ember Way',所以如果有更好的人来,请告诉我一个更好的解决方案。但暂时这对我有用。

我将JSON输出更改为以下内容(与我的问题以及我如何输出JSON相比):

{
"songs":[
    {"id":1,"name":"Song Name","artist":"Artist Name","soundcloud":"/tracks/123"},
    {"id":2,"name":"Song Name 2","artist":"Artist Name 2","soundcloud":"/tracks/124"}
],
"playlist":
    {"id":1,"name":"Playlist Name","song_ids":[1,2]}
}

我正在使用包含https://github.com/rails-api/active_model_serializers的'ember-rails'宝石(请参阅嵌入式协会部分)。

这是我的PlaylistSerializer:

class PlaylistSerializer < ActiveModel::Serializer
  embed :ids, :include => true

  attributes :id, :name
  has_many :songs, :key => :song_ids, :root => :songs
end