mercredi 29 avril 2015

Serialize records without id in embedded record mixin

I started looking on embedded record mixin for serialising/deserialising records with hasMany/belongs to relationship.

Model:

 App.Post= DS.Model.extend({

   comment: DS.belongsTo('comment'),
   name:     DS.attr('string')
 });

 App.Comment =DS.Model.extend({

  post: DS.belongsTo('Post'),
  value:   DS.attr('string')
 });

Serializer:

App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
   attrs: {
      comments: {embedded: 'always'}
   }
});

When i try to save the model,i got the following error.

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

    model.get('comments').pushObject(this.store.createRecord('comment'));


    model.setProperties({
        name: 'test'
    });

    model.save();

Error: Assertion Failed: You must include an id for App.Comment in an object passed to push . Only the error in browser console but to the backend ,data goes as expected.

So, i started looking for workaround and found something interesting in http://ift.tt/1DKPMVw.

source : http://ift.tt/1HRLv7N

On following that link, i changed my serialiser as follows.

Serializer (changed):

  App.PostSerializer=DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,DS.NoKeyMixin, {
        attrs: {
          comments: {embedded: 'always',noKey: true}
        }
     });

But when i save the model now, i am getting the floowing error.

Uncaught TypeError: embeddedRecord.serialize is not a function.

I am stuck on this and looking for help. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire