byteg
October 6, 2015, 9:46am
1
Hello everyone,
I have a model called GuestGroup. I use ActiveModelAdapter to connect to my Rails back-end. When I save the model to my backend, Ember sends the following json:
{guestGroup: {title: "test", wedding: "1"}}
As you can see, the model name is in camelCase. But I expect it to be snake_case:
{guest_group: {title: "test", wedding: "1"}}
I have 2 questions:
Why ActiveModelAdapter doesn’t convert model name to snake_case?
It reads underscored JSON from server without problems.
How can I send my model name underscored?
There is the same question but without an answer Serializer / Deserializer : different names (types) for this.store.find and model.save
Take a look here: Fit Any Backend Into Ember with Custom Adapters & Serializers - Ember Igniter
Specifically, you’ll want to write a serializer that converts your keys to snake_case
- e.g.
// app/serializers/application.js
export default DS.RESTSerializer.extend({
keyForAttribute: function(key) {
// From http://emberjs.com/api/classes/Ember.String.html#method_underscore
return Ember.String.underscore(key);
}
});
1 Like
What ember/ember data version are you using?
Are properties being snake cased, eg { guest_group: { awesome_title: 'blah' } }
?
You made need to import { ActiveModelSerializer } from 'active-model-adapter'
for your application serializer.
I think he should extend from ActiveModelSerializer
if that’s what he’s using. Although that should do it already:
https://github.com/ember-data/active-model-adapter/blob/master/addon/active-model-serializer.js#L119
1 Like
byteg
October 12, 2015, 2:10pm
5
The problem is that I’m using ember-data-url-templates. And when I redefine my Adapter, it overrides all the methods from ActiveModelSerializer.
byteg
October 12, 2015, 2:40pm
6
I’ve solved it! Thanks for your help I needed to extend ActiveModelAdapter:
import UrlTemplates from "ember-data-url-templates";
import ActiveModelAdapter from 'active-model-adapter';
export default ActiveModelAdapter.extend(UrlTemplates, {
urlTemplate: '{+host}/weddings/{weddingId}/guest_groups{/id}',
findUrlTemplate: '{+host}/guest_groups/{id}',
urlSegments: {
weddingId(type, id, snapshot, query) {
return snapshot.belongsTo('wedding', { id: true });
}
}
});
1 Like