lundi 9 janvier 2017

What is Ember Data's expected response for JsonApiSerializer + EmbeddedRecordsMixin

How should the response from the server look when doing a POST to create new records or a PATCH to update existing records, using the default JsonApiSerializer and Adapter but combined with the DS.EmbeddedRecordsMixin, to include a hasMany relationship in the request.

This is important as Ember Data uses the response to assign Id's to records after being created, and change their state.

Consider the following request, where an Invoice is created in combination with 2 invoice lines.

{
    "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"
                            }
                        },
                        "tasks": {
                            "data": []
                        }
                    },
                    "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"
                            }
                        },
                        "tasks": {
                            "data": []
                        }
                    },
                    "type": "sales-invoice-lines"
                }
            }
        ]
    }
}

In a normal GET request, Json Api includes the separate invoice lines in the included hash (see link for specification). For the Embedded Records, should the response from the POST or PATCH include the records in the included hash, or in the (in this case) sales-invoice-lines hash?

  • Does any of this even work with Ember data?
  • Are the records in the store updated with the response we get from the server?
  • Are the id's assigned?
  • How are the id's assigned? Based on the order in the array?



Aucun commentaire:

Enregistrer un commentaire