2017-08-26 18 views
0

我在這我想查詢外部API灰燼v2.14應用程序。該API需要有圓點(「」)中的鍵查詢參數,如:如何灰燼查詢中使用點字PARAMS

http://example.com/records?query.code=abc123&code.system=whatevs 

我已經試過像這樣在我的控制器建立queryParams:

// app/controllers/records.js 

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    queryParams: ['query.code', 'code.system'], 
    query.code: null, 
    code.system: null 
}) 

餘燼構建失敗,並在我的queryParams聲明後的行中的第一個點字符處出現「Unexpected token」。

我一直在使用百分號編碼來代替點嘗試,如果我在瀏覽器中輸入這工作得很好:

http://example.com/records?query%2Ecode=abc123&code%2Esystem=whatevs 

但灰燼建立,如果我嘗試%的編碼相同的再次失敗我的查詢參數在控制器中。

// app/controllers/records.js 

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    queryParams: ['query%2Ecode', 'code%2Esystem'], 
    query%2Ecode: null, 
    code%2Esystem: null 
}) 

是否有人知道我應該採取不同的做法?

回答

0

嗯,你混的東西在這裏! queryParams是關於查詢參數您的餘燼應用程序。如果你想調用外部API只是用$.ajaxember-dataember-ajaxember-fetch,根據您的具體使用情況。

的查詢參數是你的路線。因此,您可以使用像http://my-ember-app.example.com/foo?bar=baz這樣的網址,而baz則是您在控制器中使用的過濾器值。

然而,真正的問題是如何產生的一個簡單的JavaScript對象與具有點的關鍵。

答案很簡單:

var x = {}; 
x['foo.bar'] = 'bar'; 

所以你可以做這樣的事情:

const params = {}; 
params.queryParams = ['query.code', 'code.system']; 
params['query.code'] = null; 
params['query.system'] = null; 
export default Ember.Controller.extend(params) 

然而在JavaScript的新版本中,我們有一個捷徑:

var x = { 
    ['foo.bar']: 'baz' 
} 

這意味着你可以這樣做:

const params = {}; 
params.queryParams = ['query.code', 'code.system']; 
params['query.code'] = null; 
params['query.system'] = null; 
export default Ember.Controller.extend({ 
    queryParams: ['query.code', 'code.system'], 
    ['query.code']: null, 
    ['code.system']: null 
}) 

然而,這是燼查詢參數,而不是調用外部API!