- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对 Ember 很陌生,所以也许有人可以帮助我。我一直遇到这个错误,但不知道如何解决。
Ember : 2.5.1
Ember Data : 2.5.3
下面是我的router.js
。
//app/router.js
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('organization', {path: '/organization/:id'}, function(){
this.route('about', { path: '/about' });
this.route('admin', { path: '/admin' }, function(){
this.route('team', { path: '/team/:team_id' });
});
});
});
organization/:id/about
和 organization/:id/admin
路由工作正常。但是当我尝试加载 organization/:id/admin/team/:team_id
路由时,会抛出错误。以下是 routes/organization/admin/team.js
文件:
//app/routes/organization/admin/team.js
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
let organization = this.modelFor('organization');
return organization.get('team');
}
});
不太确定我应该发布哪些其他信息,因此请询问您可能认为有助于调试所需的任何其他信息。我猜这很简单,我完全没有注意到。
编辑
我添加了更多文件来帮助诊断问题:
//app/routes/organization.js
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('organization', params.organization_id)
}
});
//app/routes/organization/admin.js
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
changeValue(){
this.currentModel.save();
}
}
});
其中 currentModel
是 organization
路由的模型。我暂时删除了 organization.admin.team
模型 Hook ,只是测试
{{#link-to 'organization.admin.team' model.team.id}} 团队 {{/link-to}}
在我传递 model=model
的 organization.admin
模板中呈现的组件中。但现在,在渲染 organization.admin
模板时,我在 Javascript 控制台中遇到了相同的错误(断言失败:您需要将模型名称传递给商店的 modelFor 方法)。
最佳答案
如果将对象传递给 {{#link-to}}
帮助程序。它跳过模型 Hook 。因此,您基本上可以发送{{#link-to 'team'organization.team}}
而无需编写“model” Hook 。
“这是有道理的,它可能会保存对服务器的请求,但不可否认,它并不直观。一个巧妙的解决方法是传入它的 id,而不是对象本身” - https://www.toptal.com/emberjs/the-8-most-common-ember-js-developer-mistakes ”。
所以你应该这样做
HBs
{{#link-to 'team' organization.team.id}} Link to team management {{/link-to}}
路线
model(params) {
return this.store.findRecord('team', params.team_id)
}
关于ember.js - 断言失败: You need to pass a model name to the store's modelFor method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404203/
我是一名优秀的程序员,十分优秀!