So I have a Love
model in my EmberJS application (Facebook likes if you want to think of it that way). A reader can love a book at the following URL:
POST http://localhost:3000/loves
My method to make this is a ajax call from the Ember side is:
Ember.$.ajax({
type: "POST",
url: this.get('apiManager').requestURL('loves'),
dataType: 'json',
headers: {"Authorization": "Bearer " + jwt},
data: params
}).done((response) => {
console.log('response = ', response);
let love = this.get('store').push(response);
let book = this.get('model');
console.log('love = ', love.get('id'));
console.log('love book id = ', love.get("book"));
});
I tried setting up a love-inflector
inside app/models/love-inflector.js
like this:
import Inflector from 'ember-inflector';
const inflector = Inflector.inflector;
inflector.irregular('love', 'loves');
inflector.plural(/$/, 's');
inflector.singular(/s$/i, '');
// Meet Ember Inspector's expectation of an export
export default {};
When I try to love a book, I get the following error:
Assertion Failed: You tried to push data with a type 'loves' but no model could be found with that name.
Anybody know what steps I am missing?
Update
According to the Ember guides:
The store's push() method is a low level API which accepts a JSON API document with a few important differences from the JSON API document that the JSONAPISerializer accepts. The type name in the JSON API document must match the type name of the model exactly (In the example above the type is album because the model is defined in app/models/album.js). Attributes and relationship names must match the casing of the properties defined on the Model class.
My JSON response from the Rails server looks like this:
{
"data": {
"id": "5",
"type": "loves",
"attributes": {
"book": {
"id": 16,
"title": "Three Little Pigs",
"adult_content": false,
"author_id": 2,
"created_at": "2016-12-10T09:47:25.103Z",
"updated_at": "2016-12-10T09:47:25.103Z",
"published": true,
"cover": {
"url": "http://localhost:3000/uploads/book/cover/16/three_little_pigs.jpg"
},
"blurb": "The Three Little Pigs is a fable/fairy tale featuring anthropomorphic pigs who build three houses of different materials. A big bad wolf blows down the first two pigs' houses, made of straw and sticks respectively, but is unable to destroy the third pig's house, made of bricks."
},
...
},
"relationships": {
"book": {
"data": {
"id": "16",
"type": "books"
}
},
"sender": {
"data": {
"id": "21",
"type": "users"
}
}
}
}
}
I was hoping my inflector would pick up the "loves" in the above JSON and convert it to "love", taking care of that problem for me...but it doesn't appear that's the case...
Aucun commentaire:
Enregistrer un commentaire