简体   繁体   中英

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 ( https://guides.emberjs.com/v2.7.0/models/customizing-serializers/#toc_ids ).

Many thanks in advance!

You need to define name field to have a place to save the id.

App.Node = DS.Model.extend(
{
  // (node 'name' field used as primary key for serialization)
  name: DS.attr('string'),
  info: DS.attr('string'),
  children: DS.hasMany('node', { inverse: null })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM