[英]Specify parent in API request
I'm pretty new to ember and have been somewhat thrown in at the deep end with it so there's a good chance I'm missing something that should be incredibly obvious here. 我对炭烬还很陌生,并且已经深陷其中,所以很有可能我错过了一些在这里应该显而易见的东西。
Anyway, my rails application has defined a route as /api/1/publications/:publication_id/catalogs
. 无论如何,我的rails应用程序已将路由定义为
/api/1/publications/:publication_id/catalogs
。 This is working fine. 一切正常。
However, I cannot for the life of me get Ember to use this route, everything I try either results in a request to /api/1/catalogs
- or no request at all. 但是,我一生无法让Ember使用此路由,我尝试执行的所有操作都会导致对
/api/1/catalogs
请求-或根本没有请求。
My router.js file has: 我的router.js文件具有:
Router.map(function() {
this.route('publication', { path: '/publication/:publication_id' }, function() {
this.route('catalogs', { path: '/publication/:publication_id/catalogs' });
});
});
My publications model has 我的出版物模型有
catalogs: DS.hasMany('catalog'),
My catalog model has 我的目录模型有
publication: DS.belongsTo('publication')
And, amongst a few others lost to undo's, I have tried the following methods of querying this data in my route .js file 并且,除了其他一些因撤消而丢失的方法之外,我还尝试了以下方法来查询我的route .js文件中的数据
model() {
this.modelFor('publication.name')
var output = {
model: this.modelFor("publication")
};
let publication = this.modelFor('publication');
output.catalogs = publication.get('catalogs');
return RSVP.hash(output);
},
... ...
output.catalogs = output.model.get('/api/ember/1/publications/' + output.model.id + '/catalogs');
... ...
var output = {
model: this.modelFor("publication"),
catalogs: model.catalogs,
... ...
this.get('store').findAll('catalog', {'publication_id': output.model.id, reload: true});
Could someone please give me a pointer to what I'm doing wrong / not doing please? 有人可以告诉我我做错了/不做吗?
If I got it right you are trying to customize the URL used to query has-many
relationship catalogs
of a publication
. 如果我做对了,您正在尝试自定义用于查询
publication
has-many
关系catalogs
的URL。 Ember Data uses Adapter to configure API endpoints. Ember Data使用适配器配置API端点。 I guess your application uses
DS.RESTAdapter
. 我猜您的应用程序使用
DS.RESTAdapter
。 If so, you should implement a custom urlForFindHasMany
method . 如果是这样,则应实现一个自定义
urlForFindHasMany
方法 。 Assuming the Adapter is correctly configured to fetch publication
it may look like the following: 假设适配器已正确配置为获取
publication
则它可能如下所示:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
urlForFindHasMany(id, modelName, snapshot) {
let baseUrl = this.buildURL(modelName, id);
return `${baseUrl}/catalogs`;
}
});
The relationship should be queried via property on publication
model: 应通过
publication
模型上的属性查询该关系:
async model() {
let publication = await this.store.findRecord('publication', '1');
return publication.catalogs;
},
The catalogs
property is a DS.PromiseManyArray
, a special object that acts as as an Ember.Array
and a Promise at the same time. catalogs
属性是DS.PromiseManyArray
,这是一个特殊对象,它同时充当Ember.Array
和Promise。
Your router looks a bit odd. 您的路由器看起来有点奇怪。 Here is what it normally looks like in v3+
这是v3 +中通常的样子
this.route('products', function() {
this.route('show', { path: ‘/:id’ }, function() {
this.route('show-detail');
});
Here we have 3 routes: /products (selection screen), /products/1/show and products/1/show/show-detail. 在这里,我们有3条路线:/产品(选择屏幕),/产品/ 1 /显示和产品/ 1 /显示/显示细节。
Also, in case you do not have it, the Ember inspector chrome add-on is great at helping determine what routes/ controllers. 另外,如果您没有它, Ember检查器 chrome附加组件可以很好地帮助您确定哪些路由/控制器。 etc the Ember app is using and recognizing
等Ember应用正在使用和识别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.