This is my route model:
// routes/filter-categories.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
this.store.findAll('product');
var products = this.store.filter('products', function (product) {
console.log('filtering product %o', product.get('id'));
...
});
return products;
},
});
Basically, I need al products when I enter the filter-categories
route, and then I need to perform some client-side filtering on those products.
Taking a look at the console log, the first time I visit the filter-categories
route, I see the following:
filtering product "101"
filtering product "102"
filtering product "103"
...
filtering product "101"
...
filtering product "101"
...
filtering product "101"
...
Each product gets filtered up to 4 times on first entering (the order of processing the products is actually not deterministic). I was expecting that each product gets filtered only once!
From here on, things get even worse: each time that I enter the filter-categories
route, the filtering is performed once more for each product. So at some point I have:
filtering product "101"
filtering product "102"
filtering product "103"
...
filtering product "101"
filtering product "101"
filtering product "101"
filtering product "101"
filtering product "101"
filtering product "101"
...
filtering product "102"
filtering product "102"
filtering product "102"
filtering product "102"
filtering product "102"
filtering product "102"
...
(and now it looks deterministic)
If instead I request the products in the application route:
// routes/application.js
import Ember from 'ember';
export default Ember.Route.extend({
init() {
this._super(...arguments);
this.store.findAll('product');
},
...
});
then I start the application on the top URL and then transition to my filter-categories
route (which has no more this.store.findAll('product');
), I get the following log:
filtering product "101"
filtering product "102"
filtering product "103"
...
Which is what I was expecting. But this has the problem that I am requesting the products
in the application route, where I actually do not need them, just to work around some Ember strange behavior.
I would like to understand what is going on here, in order to implement this properly.
I assume that this has to with the fact that:
If any of a record's properties change, or if it changes state, the filter function will be invoked again to determine whether it should still be in the array.
But still I can not fully get what is going on. Why is the number of filtering rounds increasing?
Aucun commentaire:
Enregistrer un commentaire