Swyd
Swyd

Reputation: 41

How to convert JSON to store object?

I have a JSON object from my Spring backend. How do I create a data object in my Ember application store?

I tried:

createObject() {
  var _this = this;
  $.getJSON('http://localhost:8080/object/getCard?id=24').then(function(response) {
     _this.store.createRecord('cards/object/card', response);
  });
}

JSON:

{
  "id":24,
  "fullName":"qwerty",
  "form":"zzzzzzzzzzzz",
  "leader": {
    "id":23,
    "fullName":"testName test",
    "email":"emailTest"
  }
}

I have a model in Ember app

export default DS.Model.extend({
  fullName: DS.attr('String'),
  form: DS.attr('String'),
  leader: DS.belongsTo('contact', { async: true })
}

And contact model:

export default DS.Model.extend({
  fullName: DS.attr('String'),
  email: DS.attr('String')
});

Upvotes: 2

Views: 1067

Answers (1)

locks
locks

Reputation: 6577

You should use store.pushPayload instead, since the record already exists in the backend:

createObject() {
  $.getJSON('http://localhost:8080/object/getCard?id=24').then((response) => {
     this.store.pushPayload(response);
  });
}

Upvotes: 5

Related Questions