使用ActiveModelAdapter在ember中进行任意JSON查询
我正在使用Ember应用程序中的标准ActiveModelAdapter来执行大多数查询,以处理Ember模型对象 但是,在一种情况下,我希望发出任意REST请求以获取JSON以填充图表(不受模型支持),但我希望“通过”ActiveModelAdapter,以便使用正确的主机值 以下是不起作用的:使用ActiveModelAdapter在ember中进行任意JSON查询,json,ember.js,ember-data,Json,Ember.js,Ember Data,我正在使用Ember应用程序中的标准ActiveModelAdapter来执行大多数查询,以处理Ember模型对象 但是,在一种情况下,我希望发出任意REST请求以获取JSON以填充图表(不受模型支持),但我希望“通过”ActiveModelAdapter,以便使用正确的主机值 以下是不起作用的: updateChartFromIndustry: function() { Ember.$.ajax({ context: this, method: 'get', url
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(....)
启动服务器时,可能需要添加代理标志:
ember服务器启动服务器。默认端口为4200。使用--proxy标志将所有ajax请求代理到给定地址。例如,ember服务器--代理http://127.0.0.1:8080 将所有ajax请求代理到运行在的服务器http://127.0.0.1:8080.
从开始,我将此作为“一种”方式发布,但不一定是正确的方式
简而言之,适配器是通过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'),
另外,这是通过ActiveModelAdapter完成的,但是从DS.RestaAdapter派生的任何东西都应该允许相同的行为。我认为您要么用基本模型来支持图表,要么在ajax调用中包含您需要的任何身份验证内容。回想起来,我认为这是正确的答案。建立一个图表模型。我可能仍然会这样做,但目前的黑客攻击是以下。