我正在使用我的Ember应用程序中的标准ActiveModelAdapter来完成大部分查询,以处理Ember模型对象。
但是,在一种情况下,我想要发出一个任意的REST请求,以返回JSON来填充图表(不是由模型支持),但我想“遍历”ActiveModelAdapter,以便使用正确的主机值。
以下是不起作用的地方:
updateChartFromIndustry: function() {
Ember.$.ajax({
context: this,
method: 'get',
url: 'api/v3/charts/all_risk.json',
dataType: 'json',
data: { ind: this.get('ind') }
}).then(function(json) {
Ember.$('#risk-days-data').highcharts(json);
},
function(errs) {
console.log(errs);
}
);
}.observes('ind'),
在开发过程中,该查询转到localhost:4200 ( ember服务器),而不是localhost:3000的rails后端。明确地设置完整的URL可以使查询通过,但没有验证请求的各种用户会话信息。
我真的希望有一些简单的东西,比如:
this.store.query('arbitrary url and params', ....)
好像我是在对模型进行正常的查询,或者交替地利用适配器:
Ember.adapter.$.ajax(....)
发布于 2015-11-04 20:44:42
我把这作为一种“一种”方式来做,而不一定是正确的方法。
简单地说,适配器可以通过this.container.lookup('adapter:application')
获得(即使是在组件中)。它可以直接用于发送AJAX查询。我认为您可以设置自己的适配器并覆盖默认行为,但在本例中,我可以用默认适配器来欺骗它,使其以我想要的方式工作。
updateChartFromIndustry: function() {
let adapter = this.container.lookup('adapter:application');
// create the URL. Not that the 'chart' is, by default, pluralized to "charts".
// This happened to be good for my API.
// The second parameter becomes the "id" which I use to identify
// the chart name.
// http://localhost:3000/api/v3/charts/all_risk
let url = adapter.buildURL('chart', 'all_risk');
// object.data is automatically converted into query params
let params = { data: { ind: this.get('ind') } };
// make the GET request and build the Chart with the response
adapter.ajax(url, 'GET', params).then(function(response) {
Ember.$('#my-chart').highcharts(response);
});
}.observes('ind'),
P.S.这是在ActiveModelAdapter上完成的,但是从DS.RESTAdapter派生出来的任何东西都应该允许相同的行为。
发布于 2015-10-02 01:56:08
启动服务器时可能需要添加代理标志:
ember server Starts the server. The default port is 4200. Use the --proxy flag to proxy all ajax requests to the given address. For example, ember server --proxy http://127.0.0.1:8080 will proxy all ajax requests to the server running at http://127.0.0.1:8080.
https://stackoverflow.com/questions/32871993
复制相似问题