Ember.js两个API的相同资源名称

时间:2018-09-12 20:58:32

标签: javascript ember.js

ember 3.3

所需结果:有两个可用模型;余烬中的“ bc-theme”和“ ra-theme”是从两个使用资源名称“ theme”的API提取的。

我有一种简单的方法可以对一个“重命名”的模型名称进行标准化/序列化,但是还没有找到一种根据请求的API确定重命名的方法,因此无法进行适当的重命名。从提供的代码中可以看到,如果我引入另一个“主题”,则映射将失败,因为它们将是映射上的重复键以进行标准化。

没有额外的主题,映射将按预期工作。

有什么方法可以查看序列化程序中使用的适配器吗?还是因为它是由ember findRecord请求的,并且通过关系可以在每个请求上以其他方式确定主题“类型”?

后端是用Ruby on Rails编写的,也可以进行修改,但是由于API彼此独立,因此看来ember应该是处理差异的人。

// adapters/ra-theme.js
import RaAdapter from './ra';

export default RaAdapter.extend({
  pathForType() {
    return this._super('themes');
  }
});

// adapters/bc-theme.js
import bcAdapter from './bc';

export default bcAdapter.extend({
  pathForType() {
    return this._super('themes');
  }
});


// serializers/application.js
import DS from 'ember-data';

const keysMappingForSerialization = {
  'bc-theme': 'theme',
  ...
};
const keysMappingForNormalization = {
  'theme': 'bc-theme',
  ...
};

export default DS.JSONAPISerializer.extend({
  payloadKeyFromModelName(key) {
    if (keysMappingForSerialization[key]) {
      return this._super(keysMappingForSerialization[key]);
    } else {
      return this._super(...arguments);
    }
  },
  modelNameFromPayloadKey(modelName) {
    if (keysMappingForNormalization[modelName]) {
      return this._super(keysMappingForNormalization[modelName]);
    } else {
      return this._super(...arguments);
    }
  }

});

// serializers/bc-theme-group.js
import ApplicationSerializer from './application';

const keysForRelationshipsMapping = {
  'bcThemes': 'themes'
};

export default ApplicationSerializer.extend({
  keyForRelationship(key) {
    if (keysForRelationshipsMapping[key]) {
      return this._super(keysForRelationshipsMapping[key]);
    } else {
      return this._super(...arguments);
    }
  }
});

// serializers/bc-theme.js
import ApplicationSerializer from './application';

const keysForRelationshipsMapping = {
  'bcThemeGroups': 'themeGroups'
};

export default ApplicationSerializer.extend({
  keyForRelationship(key) {
    if (keysForRelationshipsMapping[key]) {
      return this._super(keysForRelationshipsMapping[key]);
    } else {
      return this._super(...arguments);
    }
  }
});

1 个答案:

答案 0 :(得分:1)

结果很简单。

我能够通过对特定于模型的序列化器/适配器进行序列化和规范化来实现此目的。如果不通过顶层应用程序序列化程序,它将仅在运行适当的序列化程序时映射所需的内容。

因此,尽管它重复了一些代码,但它可以满足我的情况。

编辑: 这是重构的代码。

// mixins/model-rename.js
import Mixin from '@ember/object/mixin';
import { computed } from '@ember/object';

export default Mixin.create({
  mappingForSerialization(acronym) {
    let mapName = acronym + 'KeysMappingForSerialization';
    return this.get(mapName);
  },
  mappingForNormalization(acronym) {
    let mapName =  acronym + 'KeysMappingForNormalization';
    return this.get(mapName);
  },

  bcKeysMappingForSerialization: computed('acronym', function() {
    return {
      'bc-theme-group': 'theme-group',
      'bc-theme': 'theme'
    };
  }),

  bcKeysMappingForNormalization: computed('acronym', function() {
    return {
      'theme-group': 'bc-theme-group',
      'theme': 'bc-theme'
    };

  }),

  radioKeysMappingForSerialization: computed('acronym', function() {
    return {
      'radio-theme': 'theme',
      'radio-tag': 'tag',
    };

  }),
  radioKeysMappingForNormalization: computed('acronym', function() {
    return {
      'theme': 'radio-theme',
      'tag': 'radio-tag'
    };
  }),

  keyForRelationship(key) {
    if (this.keysForRelationshipsMapping[key]) {
      return this._super(this.keysForRelationshipsMapping[key]);
    } else {
      return this._super(...arguments);
    }
  },

  payloadKeyFromModelName(key) {
    if (this.mappingForSerialization(this.get('acronym'))[key]) {
      return this._super(this.mappingForSerialization(this.get('acronym'))[key]);
    } else {
      return this._super(...arguments);
    }
  },
  modelNameFromPayloadKey(modelName) {
    if (this.mappingForNormalization(this.get('acronym'))[modelName]) {
      return this._super(this.mappingForNormalization(this.get('acronym'))[modelName]);
    } else {
      return this._super(...arguments);
    }
  }
});

// serializers/bc-theme-group.js
import ApplicationSerializer from './application';
import modelRename from '../mixins/model-rename';

export default ApplicationSerializer.extend(modelRename, {
  acronym: 'bc',

  keysForRelationshipsMapping(key) {
    let mapping =  {
      'bcThemes': 'themes'
    }
    return mapping[key];
  }

});



// serializers/bc-theme.js
import ApplicationSerializer from './application';
import modelRename from '../mixins/model-rename';

export default ApplicationSerializer.extend(modelRename, {
  acronym: 'bc',

  keysForRelationshipsMapping(key) {
    let mapping =  {
      'bcThemeGroups': 'themeGroups',
    }
    return mapping[key];
  }

});

// serializers/radio-theme.js
import ApplicationSerializer from './application';
import modelRename from '../mixins/model-rename';

export default ApplicationSerializer.extend(modelRename, {
  acronym: 'radio',
});


// It's also important to note that any other models that are related to the changes would also need the mappings

// serializers/episode.js
import ApplicationSerializer from './application';
import modelRename from '../mixins/model-rename';

export default ApplicationSerializer.extend(modelRename, {
  acronym: 'radio',

  keysForRelationshipsMapping(key) {
    let mapping =  {
      'radioTheme': 'theme',
      'radioTags': 'tags'
    }
    return mapping[key];
  }

});