Ember.js 余烬数据:使用serialize简化post对象,使用虚线jsonapi属性简化ATTR哈希

Ember.js 余烬数据:使用serialize简化post对象,使用虚线jsonapi属性简化ATTR哈希,ember.js,ember-data,json-api,Ember.js,Ember Data,Json Api,这里是一个简化的例子。请记住,实际模型是20个字段和一些计算属性。 “订单”模型 shippingFirstName: DS.attr('string'), shippingLastName: DS.attr('string'), 模板(作为newOrder传入的模型) 预期结果 { shipping_to: { first_name: 'sheriff', last_name: 'derek', }, } “订单”的序列化程序(我期望的工作…) 但是shi

这里是一个简化的例子。请记住,实际模型是20个字段和一些计算属性。

“订单”模型

shippingFirstName: DS.attr('string'),
shippingLastName: DS.attr('string'),

模板(作为newOrder传入的模型)


预期结果

{
  shipping_to: {
    first_name: 'sheriff',
    last_name: 'derek',
  },
}

“订单”的序列化程序(我期望的工作…)

但是shipping_to属性不会出现在帖子中,其他值是破折号(JSON:API样式)

文档很棒:但示例不是虚线键


当我玩弄attrs散列时,我可以让事情正常运行——但它似乎非常武断,或者我不明白发生了什么

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({

  serialize(snapshot, options) {
    let json = this._super(...arguments);
    json = json.data.attributes;

    console.log(json);

    json.shipping_to = {
      first_name: json.whatever_thing_not_dashed,
      last_name: json.whateverThingCamel,
    };

    delete json.whatever_thing_not_dashed;
    delete json.whateverThingCamel;

    return json;
  },

  attrs: {
    shippingFirstName: 'whatever_thing_not_dashed',
    shippingLastName: 'whateverThingCamel',
  },

});

我错过了什么?(后端上的名称不够一致,无法将camel全部改为下划线,而且它们的嵌套方式也不同)

首先,由于后端不需要JSONAPI对象,因此您应该使用另一个序列化程序,如JSONSerializer

这将删除键“data”和“type”,然后您可以使用相同的钩子在snake case中使用Ember.String.下划线转换属性,并添加所需的键

您还应该为您的模型创建一个特定的序列化程序

ember g序列化程序


这样,您只需更改所需的特定模型,而不是整个应用程序

当您直接使用
json.data.attributes
时,我会尝试测试它是否正常工作,谢谢,Marcell。我以前都用过,但我想我没有想清楚!JSONSerializer工作得很好。在这种情况下,我不能真正使用下划线方法。shippingStreetAddress≠ 发货地址为第1行。我的序列化程序已经是“order”了。如果你想尝试解释一下我上面的例子中发生的事情,那也太棒了。我很困惑…所以你会使用JSONapi序列化程序而不是JSON序列化程序?很乐意帮助:),你忘了对象格式,JSON api对象有一个特定的格式,我在这里更改了您的示例:ember twidle.com/…但不要这样做,而是使用JSONSerializer,这样您就不需要删除任何键。obj:这只是一个打字错误,我不得不重新发布它的链接:,ember twiddle是测试这类东西的好地方,我希望我知道如何更好地在chrome上格式化alertbox json消息。
{
  shipping_to: {
    first_name: 'sheriff',
    last_name: 'derek',
  },
}
import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({

  serialize(snapshot, options) {
    let json = this._super(...arguments);
    json = json.data.attributes;

    console.log(json); // shows attributes as shipping-first-name

    json.shipping_to = {
      first_name: json.shippingFirstName,
      last_name: json.shippingLastName,
    };

    delete json.shippingFirstName;
    delete json.shippingLastName;

    return json;
  },

});
import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({

  serialize(snapshot, options) {
    let json = this._super(...arguments);
    json = json.data.attributes;

    console.log(json);

    json.shipping_to = {
      first_name: json.whatever_thing_not_dashed,
      last_name: json.whateverThingCamel,
    };

    delete json.whatever_thing_not_dashed;
    delete json.whateverThingCamel;

    return json;
  },

  attrs: {
    shippingFirstName: 'whatever_thing_not_dashed',
    shippingLastName: 'whateverThingCamel',
  },

});