Ember.js两个API的资源名称相同
Ember.js two API's same resource name
ember 3.3
期望的结果:有两个模型可用; ember 中的 "bc-theme" 和 "ra-theme" 来自两个不同的 API,它们使用资源名称 "theme".
我有一个简单的方法 normalizing/serializing 一个 "rename" 的模型名称,但还没有找到一种方法来根据请求的 API 来确定重命名,因此是能够适当重命名。从提供的代码中可以看出,如果我引入另一个 "theme",映射将失败,因为它们将是用于规范化的映射中的重复键。
没有额外的主题,映射将按预期工作。
有什么方法可以查看序列化程序中使用的是什么适配器?或者因为它是由 ember findRecord 请求并通过关系可以根据每个请求以其他方式确定主题 "type" 吗?
后端是在 Rails 上用 Ruby 编写的,也可以修改,但似乎 ember 应该是处理 [=42] 之后差异的那个=] 彼此独立。
// 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);
}
}
});
原来答案很简单。
我能够通过对特定模型 serializers/adapters 进行序列化和规范化来实现这一点。通过不通过顶级应用程序序列化程序,它只会在适当的序列化程序为 运行.
时映射所需的内容
因此,虽然它重复了一些代码,但它完成了我的情况所需的内容。
编辑:
这是重构后的代码。
// 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];
}
});
ember 3.3
期望的结果:有两个模型可用; ember 中的 "bc-theme" 和 "ra-theme" 来自两个不同的 API,它们使用资源名称 "theme".
我有一个简单的方法 normalizing/serializing 一个 "rename" 的模型名称,但还没有找到一种方法来根据请求的 API 来确定重命名,因此是能够适当重命名。从提供的代码中可以看出,如果我引入另一个 "theme",映射将失败,因为它们将是用于规范化的映射中的重复键。
没有额外的主题,映射将按预期工作。
有什么方法可以查看序列化程序中使用的是什么适配器?或者因为它是由 ember findRecord 请求并通过关系可以根据每个请求以其他方式确定主题 "type" 吗?
后端是在 Rails 上用 Ruby 编写的,也可以修改,但似乎 ember 应该是处理 [=42] 之后差异的那个=] 彼此独立。
// 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);
}
}
});
原来答案很简单。
我能够通过对特定模型 serializers/adapters 进行序列化和规范化来实现这一点。通过不通过顶级应用程序序列化程序,它只会在适当的序列化程序为 运行.
时映射所需的内容因此,虽然它重复了一些代码,但它完成了我的情况所需的内容。
编辑: 这是重构后的代码。
// 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];
}
});