在我的项目中,我为具有不规则名称的模型campus
设置了一个变形器设置
出于某种原因,在从Ember CLI 0.2.7更新到Ember CLI 1.13.8后,变形器不再应用于模型。
尝试访问商店this.store.findRecord('campus', params['campus_id'])
时,我收到警告
WARNING: Encountered "campus" in payload, but no model was found for model name "campu" (resolved model name using app@serializer:campus:.modelNameFromPayloadKey("campus"))
后面跟着这个错误
No model was found for 'campu' Error: No model was found for 'campu'
返回有效负载看起来像这样
{
campus: {
id: 1,
name: "Default Campus"
},
meta: {
total: 1,
page: 1
}
}
我一直在使用以前成功的初始化程序
import Ember from 'ember';
export function initialize(/* registry, application */) {
var inflector = Ember.Inflector.inflector;
inflector.irregular('campus', 'campuses');
}
export default {
name: 'inflections',
initialize: initialize
};
我尝试更新到最新版本的Ember Data,1.13.13,并将ember inflector更新为1.9.3。我也尝试将不规则的变形器移动到模型中,但我没有更接近解决问题
我还尝试在控制台中运行以下代码,然后调用商店以查看变形器设置为
var inflector = Ember.Inflector.inflector;
inflector.singularize('campus') // returns "campus"
inflector.pluralize('campus') // returns "campuses"
这个问题非常令人沮丧,因为似乎无论如何都无法解决它。
答案 0 :(得分:0)
好吧,我能够使用EmberData-2.0.1重现这种令人困惑的行为,它看起来像是一个漏洞数据中的错误。
可能的解决方法:
//serializers/campus.js
import { ActiveModelSerializer } from 'active-model-adapter';
import Ember from "ember";
import DS from "ember-data";
const { normalizeModelName } = DS;
const { singularize } = Ember.String;
export default ActiveModelSerializer.extend({
// this is dublication from source but singuralize works as expected
modelNameFromPayloadKey: function(key) {
return singularize(normalizeModelName(key));
}
)};