2017-04-18 29 views
0

我試圖使用Ember-data中的this.store.query函數向自定義網址this.get('/transactions/from/:startDate/to/:endDate');發送GET請求。如何才能完成?使用商店獲取自定義網址的請求

+1

覆蓋您的適配器中的'query'功能 – Lux

+0

是的,這就是我最終做的。 –

回答

2

爲了發送自定義URL的GET請求,需要覆蓋適配器中的query函數。我有一個名爲transaction的型號。所以我想做類似this.store.query('transaction', { filterType: 'dateRange', startDate: '01-01-12', endDate: '12-31-12'}的事情,然後把GET請求發送到transactions/from/01-01-12/to/12-31-12。以下是我必須做的工作:

query: function(store, type, query) { 
    if (query.filterType && query.filterType === 'dateRange') { 
     const url = `transactions/from/${query.startDate}/to/${query.endDate}`; 

     return new Ember.RSVP.Promise(function(resolve, reject) { 
     Ember.$.getJSON(url).then(data => resolve(data), err => reject(err)); 
     }); 
    } else { 
     return this._super(store, type, query); 
    } 
    } 
+1

不要忘記返回'this._super(store,type,query);';)的結果 – Lux

相關問題