【发布时间】:2015-03-23 16:39:04
【问题描述】:
我在将多字模型名称与 ember-cli 结合使用时遇到问题。
我收到以下警告:
WARNING: Encountered "phrase_token" in payload, but no model was found
for model name "phraseToken" (resolved model name using
DS.ActiveModelSerializer.typeForRoot("phrase_token"))
我的查找方法如下:
store.find('phrase-token', 123).then(function(m) { ... })
我的服务器返回的 JSON 使用根 phrase_token,因为我使用的是 ActiveModelAdapter。
我的一个理论是 Ember-data 正在尝试使用 camelCase 模型名称,但是使用 dasherized 名称的 ember-cli 解析器找不到。
我还尝试了以下方法:
store.find('phraseToken', 123).then(function(m) { ... })
但是说没有这样的模型是失败的。
更新:
我注意到这只发生在单元测试中。我意识到在我的问题中我没有提到我在测试时遇到了这个问题。在我的完整应用程序中,我实际上根本没有问题。
我声明我的测试模块如下:
moduleForModel 'phrase-token', 'PhraseToken', {
# Specify the other units that are required for this test.
needs: ['adapter:application', 'serializer:application']
}
我认为这归结为在我的完整应用程序(使用完全填充的容器等)中我可以执行以下操作:
!!store.modelFactoryFor('phrase-token') # true
!!store.modelFactoryFor('phraseToken') # true
但在我的单元测试中:
!!store.modelFactoryFor('phrase-token') # true
!!store.modelFactoryFor('phraseToken') # false
更新 2:
我发现在我的单元测试设置代码中执行以下操作可以解决问题:
container.normalizeFullName = function(fullName) {
fullName.dasherize()
});
但感觉这不应该是必要的,所以我坚持要求不同的答案。
更新 3:
我为此发布了issue,ember-qunit 的 0.2.0 版本已解决此问题。
【问题讨论】:
-
是的,命名约定需要在 ember 中匹配。认为您需要更改传入的有效负载以匹配名称,或者在它进入时在 ember 端进行更改。
-
我认为活动模型适配器的重点是允许下划线名称服务器端。所以我认为我不需要改变我的有效载荷。
-
嗯,也许吧。抱歉,我没有使用活动模型序列化程序,我的错。