Note that there are some explanatory texts on larger screens.

plurals
  1. POMeteor: Is there a reactive per-template scope?
    primarykey
    data
    text
    <p>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 <code>Session.set</code>, clicking one button obviously toggles all the divs in all the template instantiations which is not desired.</p> <p>I could save the state in the data context of the template instance (which is bound to <code>this.data</code> in the <code>Template.myTemplate.rendered</code> and <code>Template.myTemplate.created</code> callbacks), but there are two problems with that.</p> <ol> <li><code>this.data</code> isn't a reactive data source, so will not propagate to the div</li> <li>I don't have access to the template instance in <code>Template.myTemplate.events</code> (as discussed on <a href="https://groups.google.com/forum/?fromgroups=#!topic/meteor-core/TA7_jDXs10Q" rel="noreferrer">meteor-core</a>)</li> </ol> <p>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.</p> <p>Am I missing something or is there really no equivalent to AngularJS's controllers that get passed a <code>$scope</code> for each template instantiation?</p> <p><strong>Update</strong>: I was thinking of something like this.</p> <p>template.html:</p> <pre><code>&lt;template name="jsonObject"&gt; &lt;input type="button" /&gt; &lt;div class="{{hidden}}"&gt;content&lt;/div&gt; &lt;/template&gt; </code></pre> <p>client.js:</p> <pre><code>Template.jsonObject.hidden = function(){ var ret = ""; if (this._hidden) { ret = "hidden"; } return ret; }; Template.jsonObject.events({ 'click input' : function(event, template){ template.data._hidden = true; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload