mercredi 26 août 2015

Ember Data EmbeddedRecordsMixin won't save newly created records

I'm using Ember Data 1.13.7 and Ember 1.13.6 with ActiveModelSerializer and EmbeddedRecordsMixin.

I have 2 models:

// models/post.js
export default DS.Model.extend({
  //bunch of attrs
  commentStuff: DS.belongsTo('commentStuff', {async:true})
}

and

//models/comment-stuff.js
export default DS.Model.extend({
  //bunch of attrs
  post: DS.belongsTo('post', {async: true))
}

in my serializer I have

export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  isNewSerializerAPI: true,

  attrs: {
    commentStuff: {serialize: 'records', deserialize: 'ids'}
  },

  keyForAttribute(attr){
    if(attr === 'commentStuff'){
      return 'comment_stuff_attributes';
    } else {
      return this._super(attr);
    }
  }

});

This works perfectly when I edit existing records and then post.save() but when I create new records with:

var post = this.store.createRecord('post');
post.commentStuff = this.store.createRecord('commentStuff');

And then fill in all their respective attributes. The json sent to the server on post.save() shows none of the commentStuff attributes and just returns null.

{post: {
  attribute1: 'whatever',
  attribute2: 'smth',
  attribute3: 4,
  comment_stuff_attributes: null}
}

Is there a different way I should be saving/creating new records?




Aucun commentaire:

Enregistrer un commentaire