admin-on-rest允許通過編寫自定義休息客戶端來消費任何JSON響應。文檔中的示例用於從json-server project消費JSON,這很簡單。React restClient admin-on-rest
我想知道在admin-on-rest中如何輕鬆地使用this api,並對restClient進行微小更改。
admin-on-rest允許通過編寫自定義休息客戶端來消費任何JSON響應。文檔中的示例用於從json-server project消費JSON,這很簡單。React restClient admin-on-rest
我想知道在admin-on-rest中如何輕鬆地使用this api,並對restClient進行微小更改。
如果我沒有記錯的話,你想擁有的url重寫從(爲例): https://marmelab.com/admin-on-rest-demo/#/customers/684 TO: https://marmelab.com/admin-on-rest-demo/?customers=684
你可以重寫restClient.js的GET_ONE: 情況下GET_ONE: url = ${apiUrl}/${resource}/${params.id}
; TO: case GET_ONE: url = ${apiUrl}/?${resource}=${params.id}
;
這將通過參數而不是url部分來獲取記錄。
好的 - 讓我們看看管理員休息的來源(文件admin-on-rest/src/util/fetch.js),我們對fetchJson方法感興趣。
該方法返回讀取的承諾,在它嘗試解析該代碼JSON:
try {
json = JSON.parse(body);
} catch (e) {
// not json, no big deal
}
,然後返回:return { status, headers, body, json };
但我們身體的結果,並可以重複使用,或者我們可能使用解析的對象JSON
爲了您的例子中,我們可以這樣做(一些代碼遺漏):
const httpClient = (url, options = {}) => {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
options.withCredentials = true;
return fetchUtils.fetchJson(url, options).then(({status, headers, body, json}) => {
json = json['results'] ? json['results'] : json;
return {status, headers, body, json};
});
}
所以我們只是通過收集overrwrited JSON對象從架構中的「結果」在該行:
json = json['results'] ? json['results'] : json;
現在你可以使用該客戶端在管理
<Admin restClient={restClient}>
...
</Admin>
警告!這將影響管理員的所有請求。但是你可以添加額外的參數。如果你不想使用json = json['results'] ? json['results'] : json;
你可以添加額外的參數或檢查方法提取
看來你的鏈接指向一個錯誤頁面 –
是的,即使對我來說鏈接不工作。請仔細查看您發佈的鏈接 – Veeresh123
他們發佈了包含更新文檔的0.4.0版本。我更新了鏈接並修復了顯示鏈接的語法問題 – Chida