So I'm in the process of building out the front end of our site, and long story short, the location of our API call is through apigility. I'm fairly new to Ember, so I've been having to piece of a lot of info together on the best way to make this work. apigility is set up to give a REST service, but the output is in the following format:
{
_links: {
self: {
href: "http://localhost:8888/article?page=1"
},
first: {
href: "http://localhost:8888/article"
},
last: {
href: "http://localhost:8888/article?page=1"
}
},
_embedded: {
article: [
{
nid: 1,
body_value: "Lorem Ipsum etc etc",
created: 1487176722,
_links: {
self: {
href: "http://localhost:8888/article/1"
}
}
}
]
},
page_count: 1,
page_size: 25,
total_items: 1,
page: 1
}
To start with since I'm kind of just trying for a proof of concept, here's my serializer:
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
primaryKey: 'nid',
normalizeResponse: function(store, primaryModelClass, payload, id, requestType) {
return {
data: payload._embedded.article,
};
}
});
I'm also using the RESTAdapter for my apadpter.
And finally my model for article.js
import DS from 'ember-data';
export default DS.Model.extend({
body_value: DS.attr('string'),
nid: DS.attr('number')
});
The error that it's currently giving me is:
You must include an 'id' for undefined in an object passed to 'push'
From that error I feel like it's correctly getting the 'article' portion from my api call, but the primaryKey portion doesn't seem to be in effect.
If I remove the contents of my serializer, I get the following list of errors:
ember.debug.js:7062WARNING: Encountered "embedded" in payload, but no model was found for model name "embedded" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("embedded"))
ember.debug.js:7062WARNING: Encountered "page_count" in payload, but no model was found for model name "page-count" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("page_count"))
ember.debug.js:7062WARNING: Encountered "page_size" in payload, but no model was found for model name "page-size" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("page_size"))
ember.debug.js:7062WARNING: Encountered "total_items" in payload, but no model was found for model name "total-item" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("total_items"))
ember.debug.js:7062WARNING: Encountered "page" in payload, but no model was found for model name "page" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("page"))
Let me know if there's any other info/code that I need to include.
EDIT
Note: The API developer did change it from 'nid' to 'id'
Sometimes you just need to start with a clean slate, huh?
This is now what my serializer looks like and it works as expected:
export default DS.RESTSerializer.extend({
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
payload = { article: payload._embedded[primaryModelClass.modelName] };
return this._super(store, primaryModelClass, payload, id, requestType);
},
});