2016-02-25 53 views
0

所以我有一個API至極的「HOUROFDAY」如何管理多個API調用反應通量應用

http://localhost:8080/api/v1/reports/61?aggregation=hourOfDay&start=2016-02-10T00:00:00.000Z&stop=2016-02-20T10:59:59.000Z 

在我提供的報告作出反應程序,我想顯示這句和前面的時間範圍。這需要對該端點執行另一個調用,以便在圖形中進行比較。

http://localhost:8080/api/v1/reports/61?aggregation=hourOfDay&start=2016-02-20T00:00:00.000Z&stop=2016-02-30T10:59:59.000Z 

那麼我該怎麼做呢?

我有一個服務 - 行動 - 商店 - 組合組合。就像這個服務被調用一樣,它會觸發兩個api調用,等待這兩個調用,然後傳遞給動作而不是存儲,所以組件可以同時訪問所有數據?或者我會如何解決這個問題?我的意思是我可以改變API端點,但我希望它儘可能乾淨,我猜想單個報告端點需要其他地方...

回答

0

我更喜歡使用一個系列。 Action - > Api.get - > Api.onGot - > Action - > Store - > Action - > Api.get - > Api.onGot - > Action - > Store and refresh。

+0

感謝您的答覆。我認爲在商店中緩存第一個結果可能是一個很好的做法。但我不確定這是否是從商店觸發行爲的正確方法。所以我想知道你是否爲我提供了更多的細節,爲什麼你會這樣做和/或任何其他的東西,如我可以閱讀的博客帖子? – Stefflan00

+0

我使用反流,因此沒有遇到任何觸發商店行爲的問題。這裏是關於發佈/訂閱的文章https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern。 –

0

我認爲更好的方法是您的視圖調用一個調用2 API請求的操作。第一個調用的請求重定向嵌套請求的結果。

看到這個僞代碼:

YourView:

// this function is called when the result2 is dispatched. 
// you can get the information from the Store 
function receiveResultFromTimeRange() { 
    console.log(YourStore.getResult2()); 
}, 

// listening for the result2 
function componentDidMount() { 
    YourStore.addChangeListener(this.receiveResultFromTimeRange); 
    YourAction.timeRange(parameter1); 
} 

YourAction:

return { 

    // you can call only the second request in this way 
    request2: function(parameter2) { 
     new RequestService().secondRequest(result1).done(function (result2) { 
      Dispatcher.dispatch({ 
       method: 'receiveResultFromSecondRequest', 
       result: result2 
      }); 
     }); 
    }, 

    // call the first request and use the result to call the second request 
    request1And2: function(parameter1){ 
     new RequestService().firstRequest(parameter1).done(function (result1) { 
      this.request2(result1); 
     }); 
    } 
} 

YourStore:

var _result2 = null; 

receiveResultFromSecondRequest: function (result2) { 
    // save result2 in the Store, LocalStorage... whatever you want. 
    _result2 = result2; 
    this.emit(SUCCESS_SECOND_REQUEST); 
}, 

addChangeListener: function (callback) { 
    this.addListener(SUCCESS_SECOND_REQUEST, callback); 
}, 

getResult2: function(){ 
    return _result2; 
}