【问题标题】:What is Ember Data's expected response for JsonApiSerializer + EmbeddedRecordsMixinEmber Data 对 JsonApiSerializer + EmbeddedRecordsMixin 的预期响应是什么
【发布时间】:2017-01-09 16:26:06
【问题描述】:

使用默认的JsonApiSerializer and Adapter,但结合DS.EmbeddedRecordsMixin,在执行POST创建新记录或PATCH更新现有记录时,来自服务器的响应应该如何看待,以包括hasMany关系请求。

这很重要,因为 Ember Data 在创建后使用响应将 ID 分配给记录,并更改它们的状态。

考虑以下请求,其中一个发票是结合 2 个发票行创建的。

{
    "data": {
        "type": "sales-invoices",
        "attributes": {
            "name": null,
            "net-price": 29,
            "vat-amount": 6.09,
            "gross-price": 35.09
        },
        "relationships": {
            "customer": {
                "data": {
                    "type": "companies",
                    "id": "131"
                }
            },
            "organization": {
                "data": {
                    "type": "organizations",
                    "id": "3"
                }
            }
        },
        "sales-invoice-lines": [
            {
                "data": {
                    "attributes": {
                        "name": "Basic Linux Hosting",
                        "sort": 0,
                        "unit-price": 9,
                        "quantity": 1,
                        "total": 9,
                        "net-price": 9,
                        "vat-amount": 1.89,
                        "gross-price": 10.89,
                        "created": null,
                        "changed": null
                    },
                    "relationships": {
                        "sales-invoice": {
                            "data": {
                                "type": "sales-invoices",
                                "id": null
                            }
                        },
                        "vat-rate": {
                            "data": {
                                "type": "vat-rates",
                                "id": "1"
                            }
                        }
                    },
                    "type": "sales-invoice-lines"
                }
            },
            {
                "data": {
                    "attributes": {
                        "name": "Extra .com domain",
                        "sort": 1,
                        "unit-price": 20,
                        "quantity": 1,
                        "total": 20,
                        "net-price": 20,
                        "vat-amount": 4.2,
                        "gross-price": 24.2,
                        "created": null,
                        "changed": null
                    },
                    "relationships": {
                        "sales-invoice": {
                            "data": {
                                "type": "sales-invoices",
                                "id": null
                            }
                        },
                        "vat-rate": {
                            "data": {
                                "type": "vat-rates",
                                "id": "1"
                            }
                        }
                    },
                    "type": "sales-invoice-lines"
                }
            }
        ]
    }
}

在正常的 GET 请求中,Json Apiincluded 哈希中包含单独的发票行(请参阅链接了解规范)。 对于嵌入式记录,来自 POST 或 PATCH 的响应是否应在包含的散列中或(在这种情况下)销售发票行散列中包含记录?

  • 这是否适用于 Ember 数据?
  • 存储中的记录是否根据我们从服务器获得的响应进行了更新?
  • id 是否已分配?
  • ID 是如何分配的?根据数组中的顺序?

【问题讨论】:

  • 我使用 jsonapiserializer 并使用 include 属性。如果您为包含的数据定义了该模型,则 ember 会识别它并将数据插入到存储中。你问的是这个吗?
  • 不,如果我发布一个帖子,在发布之前我创建了一张发票和发票行,我在我的商店中已经有(在本例中为 3 个发票 + 2 个发票行)模型;收到回复时,我想将发票的 ID 和 2 个发票行与商店中已有的模型进行匹配。这如何与 EmbeddedRecordsMixin 一起使用?

标签: ember.js ember-data json-api


【解决方案1】:

恐怕在使用 JSONAPIAdapter/Serializer 时不支持 EmbeddedRecord。您需要通过包含的哈希来执行嵌入的有效负载。

【讨论】:

  • 你能详细说明一下吗?我不明白我的 jsonapiserializer 不包含关联
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 2013-05-02
相关资源
最近更新 更多