gpt4 book ai didi

ember.js - 断言失败: You need to pass a model name to the store's modelFor method

转载 作者:行者123 更新时间:2023-12-02 19:51:13 25 4
gpt4 key购买 nike

对 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/aboutorganization/: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();
}
}
});

其中 currentModelorganization 路由的模型。我暂时删除了 organization.admin.team 模型 Hook ,只是测试

{{#link-to 'organization.admin.team' model.team.id}} 团队 {{/link-to}}

在我传递 model=modelorganization.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com