I'm connecting an Ember 2.3 front-end with a WordPress admin panel for a client. I'm using the version 2 of the WordPress JSON API.
The API is great and I've used it before with $AJAX and XHR requests, but never felt that great about my implementation and it was with version 1.
This time, I've set up an adapter like this:
app/adapters/application.js
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
host: 'http://example.com',
namespace: 'wp-json/wp/v2'
});
JSONAPIAdaper is the new default and expects the returned data to be complient to the jsonapi standard.
and then used findAll to GET the data for all of the pages into the store.
app/routes/application.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
var pagesData = this.store.findAll('page');
console.log(pagesData);
return pagesData;
}
});
Here's a URL for an example. http://ift.tt/1QlCBzX
What is returned, doesn't appear to be compliant to the jsonapi standard.
Error while processing route: work Assertion Failed: normalizeResponse must return a valid JSON API document: * One or more of the following keys must be present: "data", "errors", "meta".
Error: Assertion Failed: normalizeResponse must return a valid JSON API document: * One or more of the following keys must be present: "data", "errors", "meta".
This leads me to check out the options to serialize the data into shape. I tried a few of the options but I don't fully understand what is happening and therefor, what to do.
app/serializers/application.js
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
});
This also receives errors right off the bat, and so I think - well it's not valid jsonapi data... so I'll try the regular JSON serializer.
app/serializers/application.js
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
});
I'm unsure why, but the errors are gone and I get back an array of the pages.
For example, on the /about "view" I'll need to show some content from that json in the store / and I don't want to send another request to a server, so I try out this peekRecord() ~ which returns some page data from the store.
app/routes/about.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
var pageData = this.store.peekRecord('page', 43);
console.log(pageData);
return pageData;
}
});
In my template, I try and show some of the data I'll expect: app/templates/about.hbs
<h1>About</h1>
{{model.id}}
{{model.title}}
{{model.content}}
{{outlet}}
I can get the ID, but when I look further into the console.log of the JSON, it doesn't look how I expect it to - and none of the content I thought would be there, except the id, is there...
What adapter / serializer setup can get me where I need to be and grabbing post and page data? I would expect this combo to be prevalent.
Any direction is much appreciated. : )
(y) thank you man
RépondreSupprimer