Ember.js hasMany / belongsTo自定义外键

时间:2015-07-19 16:48:02

标签: javascript ember.js ember-data ember-cli

我想添加employee belongsTo businessbusiness hasMany employees关系,但foreign keybusinessId而不是business_id。在哪里可以配置Ember以允许businessId成为外键?

实际上,如何为所有外键设置modelId格式?

我们正在使用Ember data 1.13ember-cli 1.13

控制器/ employee.js

import DS from 'ember-data';

export default DS.Model.extend({
  business: DS.belongsTo('business', { async: true })
});

控制器/ business.js

import DS from 'ember-data';

export default DS.Model.extend({
  employees: DS.hasMany('employee', { async: true })
});

1 个答案:

答案 0 :(得分:2)

您可以实现custom serializer来转换JSON有效内容中的所需键,以匹配模型中定义的属性:

  //in app/serializers/employees
  import Ember from 'ember';
  import DS from 'ember-data';

  export default DS.RESTSerializer.extend({
    normalizeHash: {
      employees: function(hash) {
        hash.business_id = hash.businessId;
        delete hash.businessID;
      return hash;
    }
  }
  });