用服务查询参数的Ember替代方法 [英] Ember alternative to query params with service
问题描述
所以我有一个使用此模型的路线:
So I have a route with this model:
model() {
var self = this;
return RSVP.hash({
comptes : this.get('store').findAll('compte'),
contrats:
this.get('store').findAll('contrat').then(function(contrats) {
return contrats.filterBy("contrat_fk.id",
self.get('currentContract.contrat.id'));
}),
})
}
我的目标是过滤该模型与我的服务currentContract提供的值相反。
My goal is to filter the model contrat with the value provided by my service currentContract.
重新加载页面后,它可以正常工作,但是当我更改路线并返回到该路线时,该模型似乎没有被加载,我必须重新加载页面才能看到它。
It works fine when the page is reloaded, but when I change the route and comeback to this route, the model doesn't seem to be loaded and I have to reload the page to see it.
而且我真的不想使用查询参数来放置合同URL中的ID
And I don't really wan't to use query params and put the contract id in the URL
推荐答案
将过滤器逻辑移至控制器中的计算机属性。然后,当依赖项键正确时,一切都会正常工作。
Move the filter logic to a computer property in the controller. Then everything will work fine when the dependency key is right.
例如,在您的路线中,您可以这样做:
For example in your route you can do this:
model() {
return this.store.findAll('contrat');
}
然后在您的控制器中输入以下内容:
and next in your controller this:
currentContract: service(),
filteredContracts: computed('currentContract.contrat.id', 'model.@each.id', {
get() {
const cci = this.currentContract.contrat.id;
return this.model.filter(c => c.id === cci);
}
}),
注意:此代码使用。
用于吸气剂,适用于灰烬 3.1
。对于旧版本,您需要使用 .get()
。
be careful: this code uses the .
for getters, which works for ember 3.1
. For older versions you need to use .get()
.
这篇关于用服务查询参数的Ember替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!