vendredi 5 juin 2020

How to use createRecord in ember data?

I have a foo model that has a relationship with bar. foo model has no relationship with baz. However, when creating a foo record a string of id's are needed to pass. I am setting the baz but it always returns null in my request payload.

/component.js

saveFoo() {
  let foo = this.store.createRecord('foo');
  let bar = this.get('bar');

  foo.set('name', "some value");
  foo.set('bar', bar);
  foo.set('baz', "1,2,3");

  return foo.save();
}

Request payload is like this when the return foo.save() is triggered

data: {
  attributes: {
    name: "some value"
  }
  relationships: {
    bar: {data: {id: "5898"}}
    baz: {data: {id: null}}
  }
}

But the request payload should be like this. (This is how api returns 200 if my request payload is like below)

data: {
  attributes: {
    name: "some value"
  }
  relationships: {
    bar: {data: {id: "5898"}}
    baz: {data: {id: "1,2,3"}}
  }
}



Aucun commentaire:

Enregistrer un commentaire