Data results keep accumulating rather than replacing previous data set


#1

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?


#2

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.


#3

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.


#4

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?


#5

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”]}})


#6

check this clone of your example

http://meteorpad.com/pad/uCFSF7DoJPqMCYwcu/menu_depends_template_sub