【问题标题】:Ember: how to add params from router to adapterEmber:如何将参数从路由器添加到适配器
【发布时间】:2016-11-21 02:38:53
【问题描述】:

我正在尝试从以下 URL 结构中获取数据:

${ENV.APP.API_HOST}/api/v1/customers/:customer_orgCode/sites/

我正在使用适配器通过 buildURL 使用以下文件来调整请求:

// router.js
this.route('sites', { path: 'customers/:customer_orgCode/sites' }, function() {
    this.route('show', { path: ':site_id' });
});

// adapters/site.js
export default ApplicationAdapter.extend({
  buildURL (modelName, id, snapshot, requestType, query) {
    // return `${ENV.APP.API_HOST}/api/v1/customers/${snapshot???}/sites/`;
    return `${ENV.APP.API_HOST}/api/v1/customers/239/sites/`;
  }
}

// routes/sites/index.js
export default Ember.Route.extend({
  model: function() {
    let superQuery = this._super(...arguments),
        org = superQuery.customer_orgCode;
    this.store.findAll('site', org);
  }
});

我能够获取模型上的 customer_orgCode,但无法将其拉入适配器。我注意到 Ember 检查器中没有填充模型,但是当我提出请求时,站点数据存在。有谁知道我如何使用路由器上的参数中的 customer_orgCode 动态填充 buildURL?然后指定站点/索引以使用“站点”模型?

【问题讨论】:

标签: ember.js ember-data


【解决方案1】:

好的,我想通了。我需要在路线中使用query() 而不是findAll()。这允许buildURL 从路由中获取查询参数并将其作为第五个参数传递,即。 -buildURL(modelName, id, snapshot, requestType, <strong>query</strong>)。然后在路线中,我忽略了return 作为我模型设置的一部分。因此,有兴趣的人可以在下面找到解决方案。

// router.js
this.route('sites', { path: 'customers/:customer_orgCode/sites' }, function() {
    this.route('show', { path: ':site_id' });
});

// adapters/site.js
export default ApplicationAdapter.extend({
  buildURL (modelName, id, snapshot, requestType, query) {
    let org = query.org;
    return `${ENV.APP.API_HOST}/api/v1/customers/${org}/sites/`;
  }
});

// routes/sites/index.js
export default Ember.Route.extend({
  model: function() {
    let superQuery = this._super(...arguments),
        org = superQuery.customer_orgCode;
    return this.store.query('site', {org:org});
  }
});

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多