Ember: 如何将参数从路由器添加到适配器
Ember: how to add params from router to adapter
我正在尝试从以下 URL 结构中获取数据:
${ENV.APP.API_HOST}/api/v1/customers/:customer_orgCode/sites/
我正在使用适配器通过构建URL 和以下文件调整请求:
// 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?然后指定sites/index使用'site'模型?
好的,我知道了。我需要在路线中使用 query()
而不是 findAll()
。这允许 buildURL
从路由中获取查询参数并将其作为第 5 个参数传递,即。 - 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});
}
});
我正在尝试从以下 URL 结构中获取数据:
${ENV.APP.API_HOST}/api/v1/customers/:customer_orgCode/sites/
我正在使用适配器通过构建URL 和以下文件调整请求:
// 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?然后指定sites/index使用'site'模型?
好的,我知道了。我需要在路线中使用 query()
而不是 findAll()
。这允许 buildURL
从路由中获取查询参数并将其作为第 5 个参数传递,即。 - 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});
}
});