使用JSONAPI加载Ember数据端

时间:2015-07-16 00:33:18

标签: ember.js ember-data json-api

为我的路线生成的请求是http://api.myApp.com/tags/123/products,但我需要进行一些侧面加载以提高性能,所需的XHR将是:

http://api.myApp.com/tags/123/products?include=sideload1,sideload2,sideload3

我的路由器看起来像这样:

  this.route('tags', function() {
    this.route('tag', { path: ':id' }, function() {
      this.route('products', function() {

      });
    });
  });

我想为产品添加一些异步模型,目前我有:

// app/routes/tags/tag/products.js

model() {
    return this.modelFor('tags.tag').get('products');
}

我如何在路线中添加查询参数?

1 个答案:

答案 0 :(得分:1)

我在项目中做了类似的事情,store.query(type, { query });对我有用。 http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_query-and-queryrecord

在定义模型并传入

时尝试执行store.query
{include: "sideload1,sideload2,sideload3"}

另一种选择可能是为您的模型创建适配器并使用buildURL添加查询参数...但是这有点hacky并且如果您的API遵循JSON,则不应该这样做API标准。

App.TagsAdapter = DS.JSONAPIAdapter.extend({
    buildURL: function(type, id) {
        var baseURL = 'http://api.myApp.com/tags/123/products';
        var method = '?include=sideload1,sideload2,sideload3';

        return baseURL + method;
    }
});
相关问题