2016-10-10 49 views
3

admin-on-rest允许通过编写自定义休息客户端来消费任何JSON响应。文档中的示例用于从json-server project消费JSON,这很简单。React restClient admin-on-rest

我想知道在admin-on-rest中如何轻松地使用this api,并对restClient进行微小更改。

+0

看来你的链接指向一个错误页面 –

+0

是的,即使对我来说链接不工作。请仔细查看您发布的链接 – Veeresh123

+0

他们发布了包含更新文档的0.4.0版本。我更新了链接并修复了显示链接的语法问题 – Chida

回答

0

好的 - 让我们看看管理员休息的来源(文件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;你可以添加额外的参数或检查方法提取