Quantcast
Viewing all articles
Browse latest Browse all 4827

Ember embedded record mixin append related model

@Zorig wrote:

I have artist, album, track pages. Tracks are related to artist, track can have many artists. I have an artist page with listing artist's top tracks and albums. Problem is When i load album, album tracks are appended to toptracks after i updated the serializer with embeddedRecordsMixin Please help me to figure it out.

json response from backend API:

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

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4827

Trending Articles