0
votes

I'm using Ember.js 1.0.0 and Ember-Data-beta2.

I have a model Product which belongsTo Company. When creating a product, the user can select which company it belongsTo in a dropdown menu. This adds "company":"25" to the form post, which for the most part is just what I want. Instead of "company", however, I want the form to submit "company_id". How can I change this?

From what I can tell, Ember-Data serializers only normalize incoming data, not outgoing data. Would this be handled in the adapter? If so, how do I communicate this convention to Ember?

2

2 Answers

4
votes

Use the ActiveModelAdapter (see PR here):

App.ApplicationAdapter = DS.ActiveModelAdapter.extend();
4
votes

Edit: This solution is outdated, use ActiveModelAdapter instead, as suggested by Panagiotis Panagi.

With the recent versions of ember-data you have to override the serializer, in order to get "rails freindly behaivor". Like so:

// See https://github.com/emberjs/data/blob/master/TRANSITION.md
// and http://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/8
App.ApplicationSerializer = DS.RESTSerializer.extend({
    normalize: function(type, hash, property) {
        var normalized = {}, normalizedProp;

        for (var prop in hash) {
            if (prop.substr(-3) === '_id') {
                // belongsTo relationships
                normalizedProp = prop.slice(0, -3);
            } else if (prop.substr(-4) === '_ids') {
                // hasMany relationship
                normalizedProp = Ember.String.pluralize(prop.slice(0, -4));
            } else {
                // regualarAttribute
                normalizedProp = prop;
            }

            normalizedProp = Ember.String.camelize(normalizedProp);
            normalized[normalizedProp] = hash[prop];
        }

        return this._super(type, normalized, property);
    },
    serialize: function(record, options) {
        json = {}

        record.eachAttribute(function(name) {
            json[name.underscore()] = record.get(name)
        })

        record.eachRelationship(function(name, relationship) {
            if (relationship.kind == 'hasMany') {
                key = name.singularize().underscore() + '_ids'
                json[key] = record.get(name).mapBy('id')
            } else {
                key = name.underscore() + '_id'
                json[key] = record.get(name + '.id')
            }
        });

        if (options && options.includeId) {
            json.id = record.get('id') 
        }

        return json
    },
    typeForRoot: function(root) {
        var camelized = Ember.String.camelize(root);
        return Ember.String.singularize(camelized);
    },
    serializeIntoHash: function(data, type, record, options) {
        var root = Ember.String.decamelize(type.typeKey);
        data[root] = this.serialize(record, options);
    },
    serializeAttribute: function(record, json, key, attribute) {
        var attrs = Ember.get(this, 'attrs');
        var value = Ember.get(record, key), type = attribute.type;

        if (type) {
          var transform = this.transformFor(type);
          value = transform.serialize(value);
        }

        // if provided, use the mapping provided by `attrs` in
        // the serializer
        key = attrs && attrs[key] || Ember.String.decamelize(key);

        json[key] = value;
    }
});

I hope this helps.