mercredi 10 octobre 2018

ember multi level has many serialization issue

I am facing some issues with my serialization of model. Below is a detailed code which will show how my current model and corresponding serializers look.

  1. User model

    export default Model.extend({
      name: attr('string'),
      accounts: hasMany('account', {async: false})
    });
    
    
  2. Account model

     export default Model.extend({
         type: attr('string'),
         transactions: hasMany('transaction')
     })
    
    
  3. Transaction model

     export default Model.extend({
        date: attr('string'),
        amount: attr('number')
     })
    
    

So basically its a hasMany within another hasMany.

Serializers looks like this:

  1. base serializer:

     export default BaseSerializer.extend({
        keyForAttribute(key) {
          return key.underscore();
        },
        keyForRelationship(key, relationship) {
           return key.underscore();
        }
       });
    
    
  2. User serializer:

    export default BaseSerializer.extend(DS.EmbeddedRecordsMixin, {
     attrs: {
       account: { embedded: 'always' }
     }
    });
    
    
  3. Account serializer:

    export default BaseSerializer.extend(DS.EmbeddedRecordsMixin, {
     attrs: {
       transaction: { embedded: 'always' }
     }
    });
    
    

Now when I am invoking api call which gives me a json response where user model has a property named as accounts: which internally have another property called as transactions, I am expecting them to serialize but somehow its not working. have I done anything wrong here? Please advise as I am new to ember and still learning it.




Aucun commentaire:

Enregistrer un commentaire