javascript - Ember: Custom JSONAPISerializer -
i having trouble persisting data api after create new record in datastore.
// routes/application.js import ember 'ember'; export default ember.route.extend({ model(){ return this.store.findall('user'); }, actions: { test(name){ this.store.createrecord('user', { username: name, email: 'email@email.com', is_staff: false }).save(); } } });
the rest api expecting request:
{ "data": { "type": "user", "id": null, "attributes": { "username": "bill", "email": "email@email.com", "is_staff": false } } }
ember-data sending this:
{ data: { attributes: { username: "bill", email: "email@email.com", is-staff: false }, type: "users" } }
here have custom serializer, ember not seeing it. doing right?
import ds 'ember-data'; export default ds.jsonapiserializer.extend({ normalizecreaterecordresponse(store, type, payload){ return { data: { type: 'user', id: null, attributes: { username: payload.username, email: payload.email, is_staff: payload.is_staff } } } } });
on side note, make sure api working right, can send data via jquey.post():
// routes/application.js import ember 'ember'; export default ember.route.extend({ model(){ return this.store.findall('user'); }, actions: { test(name){ ember.$.post('http://localhost:8000/api/users/', { username: name, email: 'email@email.com', is_staff: false }); } });
you trying normalizecreaterecordresponse
method, when getting data server create record post call.
if want modify data sent server need:
https://guides.emberjs.com/v2.0.0/models/customizing-serializers/#toc_customizing-serializers
to use serialize
method ( there other serialize methods more fine grained control .. 1 pretty simple try out want )
this link explains it. if can't figure out there, let me know.
Comments
Post a Comment