【问题标题】:How to convert JSON to store object?如何将 JSON 转换为存储对象?
【发布时间】:2016-02-12 06:49:03
【问题描述】:

我的 Spring 后端有一个 JSON 对象。如何在 Ember 应用商店中创建数据对象?

我试过了:

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"
  }
}

我在 Ember 应用中有一个模型

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

及联系方式:

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

【问题讨论】:

    标签: json ember.js ember-data


    【解决方案1】:

    您应该改用store.pushPayload,因为记录已经存在于后端:

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

    【讨论】:

    • 谢谢,但我有一个错误:“未捕获的错误:断言失败:在调用未定义的 'push' 时,应在 'data' 属性中存在一个对象,但未定义”。跨度>
    • 嗯,您需要将 JSON 的格式与您使用的适配器所期望的格式相匹配。请参阅Model Maker 以获取参考。您可能需要implement a custom serializer。
    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 2023-03-17
    • 2017-06-16
    • 2021-11-23
    • 2016-12-31
    • 2011-03-13
    • 2016-12-26
    • 2014-08-28
    相关资源
    最近更新 更多