Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data - Saving record loses has many relationships

I am having an issue working with Ember Data Fixture Adapter. When saving a record, all of the record's hasMany associations are lost. I have created a simple JS Bin to illustrate the issue: http://jsbin.com/aqiHUc/42/edit

If you edit any of the users and save, all the projects disappear.

This is using Ember 1.0.0 and the latest canary build of Ember Data.

I am not sure if I am doing something wrong or if this is an issue with Ember Data.

Thanks

like image 214
delwyn Avatar asked Sep 30 '13 11:09

delwyn


1 Answers

To answer my question, the DS.JSONSerializer.serializeHasMany seems to only processes and serialize manyToNone and manyToMany relationship types. You can override this behaviour by using a custom serializer for the model:

var get = Ember.get;
App.UserSerializer = DS.RESTSerializer.extend({
  serializeHasMany: function(record, json, relationship) {
    var key = relationship.key;

    var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship);

    if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') {
      json[key] = get(record, key).mapBy('id');
    }
  }
});

Still not quite sure if this is a bug or if this is the desired/expected behaviour.

like image 184
delwyn Avatar answered Oct 21 '22 08:10

delwyn