首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ActiveModelAdapter在ember中进行任意JSON查询

使用ActiveModelAdapter在ember中进行任意JSON查询
EN

Stack Overflow用户
提问于 2015-09-30 17:11:35
回答 2查看 112关注 0票数 1

我正在使用我的Ember应用程序中的标准ActiveModelAdapter来完成大部分查询,以处理Ember模型对象。

但是,在一种情况下,我想要发出一个任意的REST请求,以返回JSON来填充图表(不是由模型支持),但我想“遍历”ActiveModelAdapter,以便使用正确的主机值。

以下是不起作用的地方:

代码语言:javascript
运行
复制
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可以使查询通过,但没有验证请求的各种用户会话信息。

我真的希望有一些简单的东西,比如:

代码语言:javascript
运行
复制
this.store.query('arbitrary url and params', ....)

好像我是在对模型进行正常的查询,或者交替地利用适配器:

代码语言:javascript
运行
复制
Ember.adapter.$.ajax(....) 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-04 20:44:42

我把这作为一种“一种”方式来做,而不一定是正确的方法。

简单地说,适配器可以通过this.container.lookup('adapter:application')获得(即使是在组件中)。它可以直接用于发送AJAX查询。我认为您可以设置自己的适配器并覆盖默认行为,但在本例中,我可以用默认适配器来欺骗它,使其以我想要的方式工作。

代码语言:javascript
运行
复制
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派生出来的任何东西都应该允许相同的行为。

票数 2
EN

Stack Overflow用户

发布于 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.

来自http://www.ember-cli.com/user-guide/#using-ember-cli

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32871993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档