2017-04-18 80 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