0

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!

1
  • Did you ever find a good solution to this? I'm running into something similar Commented Mar 21, 2017 at 16:18

1 Answer 1

0

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 })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ebrahim, thanks for the response! I tried this before, but this member 'name' will remain undefined. I guess Ember sees it more like an attribute member (as opposed to a top-level member).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.