Ember embedded record mixin append related model


#1

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

#2

I’ve never had this problem myself, but Ember may look for inverses to relationships. For instance, since every track has one artist, ember may look for the matching has-many relationship back from artists to tracks (the “inverse”) which is that every artist has many tracks. The only has-many relationship to track in the artist model is the toptracks one, so it may think that is the inverse when it is not. To say it is not, add an argument {inverse: null} to the original belongsTo or hasMany. (See here.)

Now, this might make sense for your code, except that in your track model, your artist property is not defined as a belongsTo the artist, which seems odd. Maybe this is a typo or change you made to fix the problem after you identified it, but I can’t think of anything else right now that might be the cause.


#3

That artist property was fully intentional thing. Anyway i solved this one like this: artists: hasMany('artist') change it to artists: attr() and this way it takes ID as a first. Also using JSON.parse in didReceivedAttrs(). But the thing is if there is json parser exist or not it works don’t understand it by now