What would be the best way to create a live filter?
Im currently using a ToggleControl bound to an observable value ( ‘selected’ ), and when the ToggleControl is toggled I want the list to be filtered, but nothing happens. What am I doing wrong? I’m still in the process of learning fuse, so bear with me. It seems like whatever combination I try using Any, Where, Flatmap, Map, the filtered list is not updated when the ‘selected’ is toggled.
var categories = new Observable(
{
id : 1,
title : "my title",
selected : new Observable(true)},
{ id : 2,
title : "my other title",
selected : new Observable(true)},
{
id : 3,
title : "my third title",
selected : new Observable(false)}
);
var subscribed_categories = categories.flatMap(function(e) {
return categories.where(function() {
return e.selected.value;
});
});
module.exports = {
subscribed_categories: subscribed_categories,
}
the reactive operators on multi-value Observables are only recalculated when there are any changes to the list itself - when an item is added or removed. In your case, you want to filter an Observable list by an Observable property that belongs to its list items - and making changes to that property does not affect the list itself in no way.
I played around with the idea and came up with the following hack’ish solution. Note that it does not (and can not) use Observables for storing the selected state, because every time I replace the items they will be completely new items and old data-bindings to those list-item-selected-properties would die.
Also, if you’re expecting to have a very large list, then this is definitely NOT the way to do it.