Sorry, I don’t know how to even word this. But here’s the problem:
http://meteorpad.com/pad/bH4otgzXAG8M5iBfj/menu_depends
When I choose animal, I see elephant and dog. When I choose object, I would expect to just see pencil and airplane, but instead it gets added to the list. Why?
Oh. I think because successive calls to Meteor.subscribe
keep adding up. I’ve changed the code to use reactive vars instead, and it works as expected:
Template.foo.helpers({
things: function() {
var className = Template.instance().className.get();
return Things.find({attrs: {$in: [className]}});
}
});
But I’m not sure which is more efficient/optimized.
Instead of subscribing in event handles, do it in Template.autorun, like this:
Template.autorun(function() {
Meteor.subscribe('things', Session.get("thing"));
});
When you change input, set the Session variable “thing” to the input value. Whenever this session variable changes, the above function will auto run. This will make Meteor unsubscribe and resubscribe to the collection. See the Meteor docs on subscription for more details.
I’m already subscribing in the Iron Router waitOn
property.
...
waitOn: function() {
return Meteor.subscribe('things', defaultValue);
}
Would that cause problems, running another subscription call in the template?
Sorry, but I’m not sure. Maybe try using Tracker.autorun and remove both subscriptions?
By the way, you can use Things.find({attrs: “animal”}) instead of Things.find({attrs: {$in: [“animal”]}})