mardi 8 novembre 2016

Ember not persisting belongsTo data

I am using Ember 2.4.6 and JSONApiAdapter of Ember for getting and sending data to the node servers.

I have the following Tags model:

import DS from 'ember-data';

export default DS.Model.extend({
  label: DS.attr('string'),
  isRoot: DS.attr('boolean', {defaultValue: false'}),
  parent: DS.belongsTo('tag') //belongs to self type
});

The logic here is that a user can enter multiple sub tags for a given root tag. And to save the sub tags and the root tags I am using a save method from tag service. The save method from tag service is as below

import Ember from 'ember';

export default Ember.Service.extend({

  //Some other code

  save: function(parent, children){
    return parent.save().then(response => {
      return children.map(tag => {
        tag.set('parent', response);
        return tag.save();
      });
    });
  }
});

Now the problem here is that when I have say five subtags for the root tag, then the relationship is getting persisted only for the last tag i.e. the fifth tag.

I have created five tags say one, two, three, four and five for the root parent tag. Then when the save method is called from the tag service then ideally the following data should be going to the servers.

{data: {id: 1, type: 'userfields', attributes: {label: "root", "is-root": true}, relationships: {parent: {data: null}}}}
{data: {id: 2, type: 'userfields', attributes: {label: "one", "is-root": false}, relationships: {parent: {data: {id: 1, type: "userfields"}}}}}
{data: {id: 3, type: 'userfields', attributes: {label: "two", "is-root": false}, relationships: {parent: {data: {id: 1, type: "userfields"}}}}}
{data: {id: 4, type: 'userfields', attributes: {label: "three", "is-root": false}, relationships: {parent: {data: {id: 1, type: "userfields"}}}}}
{data: {id: 5, type: 'userfields', attributes: {label: "four", "is-root": false}, relationships: {parent: {data: {id: 1, type: "userfields"}}}}}
{data: {id: 6, type: 'userfields', attributes: {label: "five", "is-root": false}, relationships: {parent: {data: {id: 1, type: "userfields"}}}}}

but instead the following information is going to the server

{data: {id: 1, type: 'userfields', attributes: {label: "root", "is-root": true}, relationships: {parent: {data: null}}}}
{data: {id: 2, type: 'userfields', attributes: {label: "one", "is-root": false}, relationships: {parent: {data: null}}}}
{data: {id: 3, type: 'userfields', attributes: {label: "two", "is-root": false}, relationships: {parent: {data: null}}}}
{data: {id: 4, type: 'userfields', attributes: {label: "three", "is-root": false}, relationships: {parent: {data: null}}}}
{data: {id: 5, type: 'userfields', attributes: {label: "four", "is-root": false}, relationships: {parent: {data: null}}}}
{data: {id: 6, type: 'userfields', attributes: {label: "five", "is-root": false}, relationships: {parent: {data: {id: 1, type: "userfields"}}}}}

The belongsTo information is only being sent for the last tag, and for all the other tags, the parent data is getting missed.

Not able to exactly understand why this is happening. Please suggest what I am doing wrong here




Aucun commentaire:

Enregistrer un commentaire