gpt4 book ai didi

ember.js - JSONAPI 的 Dasherize 属性

转载 作者:行者123 更新时间:2023-12-02 05:57:03 34 4
gpt4 key购买 nike

我的 drf 后端中有这个模型:

class Product:

price_range = ...

我正在将 EmberData 与 JSONApi 序列化器一起使用。我刚刚发现 JSON API 需要 dasherized 属性。所以我需要告诉 drf:

JSON_API_FORMAT_KEYS = 'dasherize'

该属性在 JSON 中序列化为:

price-range

然后,EmberData 开始跳舞,我得到了 Ember 模型属性:

import DS from 'ember-data';

export default DS.Model.extend({
...
priceRange: DS.attr('number'),
...
});

(旧的 RESTSerializer 期望 JSON 中的 priceRange,如果我没记错的话)

所以,我们从 price_range -> price-range -> priceRange (如果你问我的话,这相当疯狂)。我通过反复试验发现了这一切。对于 drf,相应的设置记录在 here 中.

JSONApiEmberDataEmber 的文档在哪里?我想确保我真的理解了这一点,而且人际关系也是如此。相关的 drf 配置设置为:

JSON_API_FORMAT_RELATION_KEYS = 'dasherize'

最佳答案

blog post for the Ember-Data 1.13 release Ember 数据团队写道:

JSONSerializer and RESTSerializer have been refactored and streamlined to return JSON API payloads.

这意味着 Ember Data 的发展需要与 JSON API 一致的短划线名称。请参阅the JSON API recommendation for dashes in between words of member names :

Member names SHOULD contain only the characters "a-z" (U+0061 to U+007A), "0-9" (U+0030 to U+0039), and the hyphen minus (U+002D HYPHEN-MINUS, "-") as separator between multiple words.

并查看 JSON API 主页上的一些示例 JSON:

-"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},

就型号而言,the most recent documentation for Ember 2.2.0状态:

In Ember Data the convention is to camelize attribute names on a model

使用多世界属性名称给出的示例模型符合预期:

export default DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),

isPersonOfTheYear: DS.attr('boolean')
});

虽然推荐的命名约定肯定发生了变化,但我预计此时大部分更改都已成为过去。核心团队已经注意到了。我相信围绕 JSON API 进行标准化的举措,以便可以构建可互操作和可重用的工具,但不幸的是,这一举措伴随着这些变化。

总之:在 JSON 中使用破折号名称,在模型中使用驼峰名称。

编辑:看起来,虽然 Ember Data 团队采用了有关 JSON 有效负载中的属性名称的 JSON API 建议,但这只是一个建议。请参阅 GitHub regarding the dasherized naming convention 上的讨论用于 JSON API。

关于ember.js - JSONAPI 的 Dasherize 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34546099/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com