vendredi 19 août 2016

Ember.js findRecord() called with id null for model with alternative primary key

I have an issue with my Ember.js application. It uses the JSONAPI{Adapter,Serializer} and the following model:

models/node.js

App.Node = DS.Model.extend(
{
  // (node 'name' field used as primary key for serialization)

  info: DS.attr('string'),
  children: DS.hasMany('node', { inverse: null })
}

Which represents a tree of named nodes.

The JSONAPIAdapter (adapters/application.js) implements the functions queryRecord(), query(), findRecord(), findAll() to translate the Ember.js queries from the server. This all works fine.

The JSONAPISerializer implements the function normalizeResponse() to translate the server response JSON data to json:api format. In the serializer, the primary key is defined to be the 'name' field of the node:

serializers/application.js

App.ApplicationSerializer = DS.JSONAPISerializer.extend(
{
  primaryKey: 'name',

  normalizeResponse(store, primaryModelClass, payload, id, requestType)
  {
    // ...
  }
});

A sample of the json:api data generated by the serializer is:

{
  "data": [
    {
      "type": "node",
      "attributes": {
        "info": "Root node"
      },
      "relationships": {
        "children": {
          "data": [
            {
              "type": "node",
              "name": "Root/SubNode1"
            }
          ]
        }
      },
      "name": "Root"
    }
  ],
  "included": [
    {
      "type": "node",
      "attributes": {
        "info": "Subnode 1"
      },
      "relationships": {
        "children": {
          "data": [
          ]
        }
      },
      "name": "Root/SubNode1"
    }
  ]
}

I use Ember.js version 2.7.0 and Ember inspector.

Once the application is running, and data is loaded in the model, I can see the data in Ember inspector being visible in the model. However, when investigating the model data in the 'Data' view (and selecting an item), I find that Ember is invoking adapter:findRecord() with id = null, resulting in an erroneous query. Somehow it seems the model data is incorrect.

When I remove the primary key definition in the JSONAPISerializer and duplicate the name field of a node in the id field as the Ember default primary key, all is fine. What am I missing with my primary key definition? The Ember guide only states information about the primaryKey in the serializer (http://ift.tt/2b3I0V3).

Many thanks in advance!




Aucun commentaire:

Enregistrer un commentaire