2017-05-08 34 views
1

我是REST管理員中的新成員。 我的/users反應是像這樣的:REST上的管理員如何設置列表元素的路徑

{ 
    "status": 200, 
    "response": { 
    "data": [ 
     { 
     "id": 298487355, 
     "login": "000000000053" 
     }, 
     ... 
    ] 
    } 
    "error": "text error" 
} 

我怎麼可以設置response路徑:data[...]讓用戶的列表? 謝謝

回答

2

您可以自定義您的restClient。例如我選擇與jsonServer工作,所以我有這個在app.js

import customRestClient from './customRestClient' 
const App =() => (
    <Admin restClient={customRestClient('http://path.to.my.api/')}> 

customRestClient實際上是this文件,我把它給我的源代碼,調整進口。

這個文件是你的數據來自並從你的應用程序到你的api的點。

所以在convertHTTPResponseToREST功能你根本就檢查resource,如果這是users您可以通過 json.response.data訪問您的數據和switch

+0

感謝,我會嘗試 – Alexey

+0

是的,它的工作原理。我很感謝你@pelak。你拯救我的一天。以及您如何看待改進此功能的管理到休息的提案,或者它如此簡單並且不值得? – Alexey

+0

你節省了我的一天 –

0

感謝@pelak很多返回。 我只是寫你的答案的代碼庫。

在您的自定義restClient指出響應數據的路徑。

const convertHTTPResponseToREST = (response, type, resource, params) => { 
    const { headers, json } = response; 
    switch (type) { 
     case GET_LIST: 
     case GET_MANY_REFERENCE: 
      if (!headers.has('content-range')) { 
       throw new Error(
        'The Content-Range header is missing in the HTTP Response. The simple REST client expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?' 
       ); 
      } 
      // IF you just want use with **users** resource. 
      if(resource === 'users') { 
       return { 
        data: json.result, 
        total: parseInt(
         headers 
          .get('content-range') 
          .split('/') 
          .pop(), 
         10 
        ), 
       }; 
      } 
      return { 
       data: json.result, 
       total: parseInt(
        headers 
         .get('content-range') 
         .split('/') 
         .pop(), 
        10 
       ), 
      }; 
     case CREATE: 
      return { data: { ...params.data, id: json.id } }; 
     default: 
      return { data: json }; 
    } 
}; 

關鍵字:列表子元素,列表窩