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