在 API 请求中指定父级
Specify parent in API request
我是 ember 的新手,对它有些了解,所以我很可能遗漏了一些在这里应该非常明显的东西。
无论如何,我的 rails 应用程序已将路由定义为 /api/1/publications/:publication_id/catalogs
。这工作正常。
但是,我终生无法 Ember 使用这条路线,我尝试的所有操作要么导致对 /api/1/catalogs
的请求 - 或者根本没有请求。
我的 router.js 文件有:
Router.map(function() {
this.route('publication', { path: '/publication/:publication_id' }, function() {
this.route('catalogs', { path: '/publication/:publication_id/catalogs' });
});
});
我的出版物模型有
catalogs: DS.hasMany('catalog'),
我的目录模型有
publication: DS.belongsTo('publication')
而且,在其他一些因撤消而丢失的数据中,我尝试了以下方法在我的路由 .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});
有人可以指点一下我做错了什么/没做吗?
您的路由器看起来有点奇怪。这是它在 v3+
中通常的样子
this.route('products', function() {
this.route('show', { path: ‘/:id’ }, function() {
this.route('show-detail');
});
这里我们有 3 条路线:/products(选择屏幕)、/products/1/show 和 products/1/show/show-detail。
此外,如果您没有,Ember inspector chrome 附加组件非常适合帮助确定哪些路由/控制器。等等 Ember 应用正在使用和识别
如果我没看错,您正在尝试自定义 URL 用于查询 publication
的 has-many
关系 catalogs
。 Ember 数据使用适配器配置 API 个端点。我猜你的应用程序使用 DS.RESTAdapter
. If so, you should implement a custom urlForFindHasMany
method。假设适配器已正确配置为获取 publication
,它可能如下所示:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
urlForFindHasMany(id, modelName, snapshot) {
let baseUrl = this.buildURL(modelName, id);
return `${baseUrl}/catalogs`;
}
});
应通过 属性 在 publication
模型上查询关系:
async model() {
let publication = await this.store.findRecord('publication', '1');
return publication.catalogs;
},
catalogs
属性是一个DS.PromiseManyArray
,一个同时充当Ember.Array
和Promise的特殊对象。
我是 ember 的新手,对它有些了解,所以我很可能遗漏了一些在这里应该非常明显的东西。
无论如何,我的 rails 应用程序已将路由定义为 /api/1/publications/:publication_id/catalogs
。这工作正常。
但是,我终生无法 Ember 使用这条路线,我尝试的所有操作要么导致对 /api/1/catalogs
的请求 - 或者根本没有请求。
我的 router.js 文件有:
Router.map(function() {
this.route('publication', { path: '/publication/:publication_id' }, function() {
this.route('catalogs', { path: '/publication/:publication_id/catalogs' });
});
});
我的出版物模型有
catalogs: DS.hasMany('catalog'),
我的目录模型有
publication: DS.belongsTo('publication')
而且,在其他一些因撤消而丢失的数据中,我尝试了以下方法在我的路由 .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});
有人可以指点一下我做错了什么/没做吗?
您的路由器看起来有点奇怪。这是它在 v3+
中通常的样子this.route('products', function() {
this.route('show', { path: ‘/:id’ }, function() {
this.route('show-detail');
});
这里我们有 3 条路线:/products(选择屏幕)、/products/1/show 和 products/1/show/show-detail。
此外,如果您没有,Ember inspector chrome 附加组件非常适合帮助确定哪些路由/控制器。等等 Ember 应用正在使用和识别
如果我没看错,您正在尝试自定义 URL 用于查询 publication
的 has-many
关系 catalogs
。 Ember 数据使用适配器配置 API 个端点。我猜你的应用程序使用 DS.RESTAdapter
. If so, you should implement a custom urlForFindHasMany
method。假设适配器已正确配置为获取 publication
,它可能如下所示:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
urlForFindHasMany(id, modelName, snapshot) {
let baseUrl = this.buildURL(modelName, id);
return `${baseUrl}/catalogs`;
}
});
应通过 属性 在 publication
模型上查询关系:
async model() {
let publication = await this.store.findRecord('publication', '1');
return publication.catalogs;
},
catalogs
属性是一个DS.PromiseManyArray
,一个同时充当Ember.Array
和Promise的特殊对象。