Ember嵌入式记录mixin附加相关模型

时间:2016-07-20 04:18:00

标签: javascript ember.js ember-data ember-cli

我有艺术家,专辑,曲目页面。曲目与艺术家有关,曲目可以有很多艺术家。我有一个艺术家页面,列出了艺术家的顶级曲目和专辑。问题是当我加载相册时,在用embeddedRecordsMixin更新序列化程序后,相册曲目会附加到toptracks上。请帮我弄明白。

来自后端API的json响应: "id": 12, "title": "test track", "artist": "test artist", "artists": [ { "id": 168, "name": "test artist" } ] },

//serializer, application serializer is a RESTAPISerializer
export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
  attrs: {
    artists: {
      deserialize: 'ids',
      serialize: false
    }
  }
});

//artist model
export default Model.extend({
  active: attr('boolean'),
  name: attr(),
  cover: attr(),
  like_count: attr(),
  releases: hasMany('album'),
  toptracks: hasMany('track'),
  albums: Ember.computed.filterBy('releases', 'single', false).readOnly(),
  singles: Ember.computed.filterBy('releases', 'single', true).readOnly(),
});

//album model
    export default Model.extend({
  title: attr(),
  artist: attr(),
  single: attr('boolean'),
  release_date: attr('string'),
  cover: attr(),
  like_count: attr('number'),
  tracks: hasMany('track', {async:true}),
  artists: attr(),
  discs: Ember.computed('tracks.[]', function(){
    return this.get('tracks').reduce(function(disc, track) {
      var id = track.get('disc');
      if (disc[id]) {
        disc[id].tracks.push(track);
      } else {
        disc[id] = {'tracks': [track]};
      }
      return disc;
    }, {});
  }).readOnly(),
  groupDiscs: Ember.computed('tracks.[]', function() {
    return _.uniq(this.get('tracks').mapBy('disc')).length > 1;
  }).readOnly()
});

//track model
export default Model.extend({
  album: belongsTo('album'),
  icon: attr(),
  title: attr(),
  artist: attr(),
  explicit: attr('boolean'),
  duration: attr(),
  disc: attr('number'),
  seq: attr('number'),
  active: attr('boolean'),
  like_count: attr('number'),
  minutes: Ember.computed('duration', function() {
    var duration = this.get('duration');
    if (duration) {
      return duration.substring(3);
    }
  }),
  playing: false,
  artists: hasMany('artist')
});

1 个答案:

答案 0 :(得分:0)

更新获得了解决方案 更改跟踪模型中的artists字段: artists: hasMany('artists')artists: hasMany('artists')并在didReceiveAttrs()

中使用JSON.parse