Meteor: Is there a reactive range per model?


I'm rendering the same Handlebars template in multiple (arbitrarily many) locations on the same page. Inside each template, I want a button to toggle the visibility of a div. When I save this state with Session.set, clicking one button obviously toggles all the divs in all the template instantiations which is not desired.

I could save the state in the data context of the template instance (which is bound to in the Template.myTemplate.rendered and Template.myTemplate.created callbacks), but there are two problems with that.

  1. isn't a reactive data source, so will not propagate to the div
  2. I don't have access to the template instance in (as discussed on meteor-core)

Finally, I could somehow save it to a collection. But how would I uniquely identify the state for each template rendered? There might also be a hacky way with jQuery, but that's not the way I want to develop Meteor apps that are supposed to work with reactive templates. Especially, when that template gets more complex.

Am I missing something or is there really no equivalent to AngularJS's controllers that get passed a $scope for each template instantiation?

Update: I was thinking of something like this.


<template name="jsonObject">
    <input type="button" />
    <div class="{{hidden}}">content</div>


Template.jsonObject.hidden = function(){
    var ret = "";
    if (this._hidden) {
        ret = "hidden";
    return ret;
    'click input' : function(event, template){ = true;

All other answers are too complicated and/or outdated. As of Meteor 1.0, the recommended solution to maintaining per-template state is to use reactive variables stored in the template instance:

A template instance object represents an occurrence of a template in the document. It can be used to access the DOM and it can be assigned properties that persist as the template is reactively updated. [...] you can assign additional properties of your choice to the object.

Reactive variables are provided by the reactive-var core package:

A ReactiveVar holds a single value that can be get and set, such that calling set will invalidate any Computations that called get, according to the usual contract for reactive data sources.

Your code becomes:

Template.jsonObject.onCreated = function () {
  this.hidden = new ReactiveVar(false);

  hidden: function () {
    return Template.instance().hidden.get() ? 'hidden' : '';
  'click input': function (event, template) {

The HTML is the same as you'd expect:

<template name="jsonObject">
  <button>Click Me</button>
  <p>Hidden is {{hidden}}.</p>