ember.js - When is filtering in Ember performed? -
this route model:
// routes/filter-categories.js import ember '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, need al products when enter filter-categories
route, , need perform client-side filtering on products.
taking @ console log, first time visit filter-categories
route, see following:
filtering product "101" filtering product "102" filtering product "103" ... filtering product "101" ... filtering product "101" ... filtering product "101" ...
each product gets filtered 4 times on first entering (the order of processing products not deterministic). expecting each product gets filtered once!
from here on, things worse: each time enter filter-categories
route, filtering performed once more each product. @ point 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 looks deterministic)
if instead request products in application route:
// routes/application.js import ember 'ember'; export default ember.route.extend({ init() { this._super(...arguments); this.store.findall('product'); }, ... });
then start application on top url , transition filter-categories
route (which has no more this.store.findall('product');
), following log:
filtering product "101" filtering product "102" filtering product "103" ...
which expecting. has problem requesting products
in application route, not need them, work around ember strange behavior.
i understand going on here, in order implement properly.
i assume has with fact that:
if of record's properties change, or if changes state, filter function invoked again determine whether should still in array.
but still can not going on. why number of filtering rounds increasing?
i think order of code wrong findall asynchonous call it´s not done after called it, there´re several options handle this
1. wouldn´t this.store.findall('product').then(function(results) { var filtedresults = results.filter(function(product) { if (condition) { return true; //added array } else { return false; } }); return filteredresults; }); 2. route model() { return this.store.findall('product'); } // in controller *value=value observe changes determine if model should contained in products array filteredproducts: ember.computed('model.[]', 'model.@each.value', function() { var filtedresults = this.get('model').filter(function(product) { if (condition) { return true; //added array } else { return false; } }); return filtedresults; }), //template {{#each filteredproducts |product|}} {{product}} {{/each}}
Comments
Post a Comment