Serializer / Deserializer : different names (types) for this.store.find and model.save


#1

Hello, I’ve got a weird serializer issue with Ember-Data.

I want my models’ names to be the same as in the API :
if the table in the db is called address_postal, I want to name my model address_postal ;
and Ember Data will hit the address_postals route on the API.

I tweaked the RESTAdapter to fit my needs (using Ember App Kit syntax)

// app/adapters/application.js

// create a basic inflector : just adds 's'
var rules = {
  plurals: [ [/$/, 's'] ],
  singular: [ [/s$/i, ''] ]
};
var inflector = new Ember.Inflector(rules);

// not sure whether this is actually useful
DS.ActiveModelAdapter.reopen({
  pathForType: function(type) {
    var decamelized = Ember.String.decamelize(type);
    var underscored = Ember.String.underscore(decamelized);
    return inflector.pluralize(underscored);
  }
});

// custom RESTAdapter : set host
var MyAdapter = DS.RESTAdapter.extend({
  host: 'http://my.host.com:3000'
});

MyAdapter.reopen({
  // model names
  pathForType: function(type) {
    var decamelized = Ember.String.decamelize(type);
    var underscored = Ember.String.underscore(decamelized);
    return inflector.pluralize(underscored);
  },

  generateIdForRecord: function(store, record) {
    // from node-uuid
    return uuid.v4();
  }
});

export default MyAdapter;

I’m not sure this is all necessary (maybe pathForType is redundant ?), but it works so far at least one way around, to get data from the API.

However when I want to save data, the object sent to the server is transformed, and address_postal becomes addressPostal. I know that’s Ember Data default behavior, but I don’t want that, and I don’t understand why find and save wouldn’t use the same serializer/deserializer.

What function should I override to get the desired behavior ?

Any clue welcome !


Underscore model name in JSON while saving model