使用Ember.js和Ember-Data序列化传出数据

时间:2013-10-06 05:05:26

标签: ember.js ember-data

我正在使用Ember.js 1.0.0和Ember-Data-beta2。

我有一个属于Product的模型Company。创建产品时,用户可以在下拉菜单中选择所属的公司。这会将"company":"25"添加到表单帖子中,这大部分都是我想要的。但是,我希望表单提交"company",而不是"company_id"。我怎么能改变这个?

据我所知,Ember-Data序列化程序仅规范化传入数据,而不是传出数据。这会在适配器中处理吗?如果是这样,我如何将此约定传达给Ember?

2 个答案:

答案 0 :(得分:4)

编辑:此解决方案已过时,请使用ActiveModelAdapter,如Panagiotis Panagi所建议。

使用最新版本的ember-data,您必须覆盖序列化程序,以获得“rails freindly behaivor”。像这样:

// 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;
    }
});

我希望这会有所帮助。

答案 1 :(得分:4)

使用ActiveModelAdapter(请参阅PR here):

App.ApplicationAdapter = DS.ActiveModelAdapter.extend();
相关问题