简体   繁体   English

在API请求中指定父项

[英]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.

相关问题 Javascript REST API - 您必须指定 API 密钥才能发出请求 - Javascript REST API - You must specify an API key to make request 防止 GoogleJsonResponseException:对 sheet.spreadsheets.batchUpdate 的 API 调用失败并出现错误:必须至少指定一个请求 - Preventing GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error: Must specify at least one request 如何在 CKAN “datastore_create” API 请求中指定标签和描述字段 - How to specify label and description fields in CKAN "datastore_create" API request Google Shopping API node.js产品插入返回“INSERT请求必须指定产品”错误 - Google Shopping API node.js product insert returns “INSERT request must specify product” error 在Google API调用中指定请求正文(使用适用于JavaScript的Google API客户端库) - Specify request body in Google API calls (using Google APIs Client Library for JavaScript) 如何在 Google 表格 API node.js 批量更新请求上指定确切的表格 - How to specify exact sheet on Google Sheets API node.js batchUpdate request 获取指定父母的孩子的属性? - Get attribute of children of a specify Parent? 指定请求桌面站点位置 - Specify Request Desktop Site Location 如何在XDR请求中指定dataType? - how to specify dataType in XDR request? 在发出API请求时将函数错误从父组件传播到子组件 - Propagate function error from parent component to child component in React while making API request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM