javascript - Meteor - Simple Search Issues -
so i'm trying live search of client-side info in meteor.
i have
template.usertable.events({ "change #namesearchbar":function(event){ //console.log(event); searchtext = event.target.value; filteredusers = meteor.users.find({"profile.name":{$regex: (".*"+searchtext+".*") } }); console.log(filteredusers.fetch()); } });
in js, and
template.usertable.helpers({ usersonline: function() { return filteredusers; } });
as well. can see filteredusers updated in console logs, don't nice live-update of html lists usersonline - instead of them, usersonline initialised to, calling filteredusers = meteor.users.find().
how can desired live-update?
your filteredusers
variable not reactive, when changes, nothing telling usersonline
helper re-run. think can in 1 of 2 ways:
use reactivevar. i'm admittedly not experienced them, think assign reactivevar part of template, , have watch -- like:
template.usertable.created = function() { this.data.filteredusers = new reactivevar(...) // initialize whatever } template.usertable.helpers({ usersonline: function() { return this.filteredusers.get(); // pulling reactive var rather static var } }); template.usertable.events({ "change #namesearchbar":function(event){ searchtext = event.target.value; // setting reactive var should invalidate "get" in helper , trigger re-run filteredusers.set(meteor.users.find({"profile.name":{$regex: (".*"+searchtext+".*") } })); } });
use session variable -- similar, it's accessible globally instead of set on template. session variables reactive default:
template.usertable.created = function() { session.setdefault('filteredusers', ...) // initialize whatever } template.usertable.destroyed = function() { session.set('filteredusers', null); // clean after when navigate away } template.usertable.helpers({ usersonline: function() { return session.get('filteredusers'); // pulling session var, reactive } }); template.usertable.events({ "change #namesearchbar":function(event){ searchtext = event.target.value; // setting session var should invalidate "get" in helper , trigger re-run session.set('filteredusers', meteor.users.find({"profile.name":{$regex: (".*"+searchtext+".*") } })); } });
like said, haven't done lot reactivevars, think #1 technically better way go, i'd play around first.
Comments
Post a Comment