获取错误:您需要将模型名称传递给商店的modelFor方法 [英] Getting error: You need to pass a model name to the store's modelFor method
问题描述
使用Ember 2.6.0
Using Ember 2.6.0
确定以下路线:
Router.map(function() {
this.route('brands');
this.route('brand', {path: '/brands/:brand_id'});
});
品牌
的型号是: p>
The model for brand
is:
export default Model.extend({
name: attr('string'),
description: attr('string'),
dateCreated: attr('date'),
lastUpdated: attr('date'),
regions: hasMany('region')
});
而区域
的模型是: / p>
And the model for region
is:
export default Model.extend({
name: attr('string'),
dateCreated: attr('date'),
lastUpdated: attr('date'),
brand: belongsTo('brand')
});
现在,尝试访问 /品牌
我在路线上这样做:
Now, when trying to access /brands
I am doing this in the route:
export default Ember.Route.extend({
model() {
return this.store.findAll('brand');
}
});
我收到以下错误:
您需要将模型名称传递给商店的modelFor方法
You need to pass a model name to the store's modelFor method
code>品牌路线和区域
关系。品牌是父母,所以我不知道为什么这不工作。
This worked prior to adding the brand
route and region
relationships. Brand is the parent, so I'm not sure why this isn't working.
更新:
从品牌
中删除区域:hasMany('region')
允许事情再次工作。不知道为什么它不能与定义的关系使用。
Removing regions: hasMany('region')
from the brand
model allows things to work again. Not sure why it isn't working with the defined relationship.
推荐答案
所以原来我必须为每个对象创建序列化。对于品牌我有:
So it turns out I had to create serializers for each object. For brand I have:
import RESTSerializer from 'ember-data/serializers/rest';
import DS from 'ember-data';
export default RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
regions: {embedded: 'always'}
}
});
而对于区域
我有: p>
And for region
I have:
import RESTSerializer from 'ember-data/serializers/rest';
export default RESTSerializer.extend({
});
从技术上讲,它没有区域序列化程序,但是我收到一个警告,即ember找不到$ 区域内的c $ c>区域
。我可以向ember提交一张机票,建议不要创建一个空的 RESTSerializer
,以避免此警告。
Technically, it works without the region serializer but I get a warning that ember couldn't find regions
inside the region
model. I may submit a ticket to ember to suggest not having to create an empty RESTSerializer
just to avoid this warning.
这篇关于获取错误:您需要将模型名称传递给商店的modelFor方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!