[英]Javascript REST API - You must specify an API key to make request
[英]Specify parent in API request
我對炭燼還很陌生,並且已經深陷其中,所以很有可能我錯過了一些在這里應該顯而易見的東西。
無論如何,我的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')
並且,除了其他一些因撤消而丟失的方法之外,我還嘗試了以下方法來查詢我的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});
有人可以告訴我我做錯了/不做嗎?
如果我做對了,您正在嘗試自定義用於查詢publication
has-many
關系catalogs
的URL。 Ember Data使用適配器配置API端點。 我猜您的應用程序使用DS.RESTAdapter
。 如果是這樣,則應實現一個自定義urlForFindHasMany
方法 。 假設適配器已正確配置為獲取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。
您的路由器看起來有點奇怪。 這是v3 +中通常的樣子
this.route('products', function() {
this.route('show', { path: ‘/:id’ }, function() {
this.route('show-detail');
});
在這里,我們有3條路線:/產品(選擇屏幕),/產品/ 1 /顯示和產品/ 1 /顯示/顯示細節。
另外,如果您沒有它, Ember檢查器 chrome附加組件可以很好地幫助您確定哪些路由/控制器。 等Ember應用正在使用和識別
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.