So I’m trying to use a concept from the simple todos, having a button set a collection object’s private variable to false or true. However the button will only change the private variable once each time the template is loaded instead of being able to toggle the private setting multiple times within one template instance. What am I doing wrong/overlooking?
<template name="mapInfo">
{{# if ownMap}}
<form>
<div class="form-group">
<label class="control-label" for="title">Map Title</label>
<div class="controls">
<input name="title" id="title" type="text" value="{{map.title}}" placeholder="Name Your Map" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label" for="mapInfo">About this Map</label>
<div class="controls">
<input name="mapInfo" id="mapInfo" type="text" value="{{map.mapInfo}}" placeholder="What's in this map?" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label" for="privacy-settings">Privacy Settings</label>
<p>A private map can only be viewed if a person has a copy of the URL. It will not show up on the Public Maps page.</p>
<button class="toggle-private" data-action="setPrivate">
{{#if map.private}}
Make Public
{{else}}
Make Private
{{/if}}
</button>
</div>
<hr/>
<input type="submit" value="Submit" class="btn btn-primary submit"/>
<a class="btn btn-danger delete" href="#">Delete Map</a>
</form>
{{else}}
<h3>
{{map.title}}
</h3>
<p><strong>created by: {{map.author}}</strong></p>
<p>{{map.mapInfo}}</p>
{{/if}}
</template>
Typically the map item would be passed in from a reactive source in the parent template, and as a template argument (i.e. this.map) it stays updated.
In this case you have the map helper which retrieves the reactive map item, but the event handler refers to a different variable currentMap, whose private property may not be updated, so that you’re continually passing the same value to the method for the setToPrivate argument. As a quick hack, you could replace your method call with
Longer term you’d probably want to store the reactive value on the instance so that you can reference it from the event handler rather than re-running the collection query.
Btw, findOne is reactive by default, so I don’t think there’s actually a problem with the helper.
You were right findOne was not reactive, but it seems they changed this behavior, it’s now equivallent to find().fetch()[0]. Weird, I could have swore findOne is not reactive.
It’s always been equivalent to find().fetch()[0], but I honestly cannot recall whether the behaviour’s changed. It would be odd if it had though - MDG are thorough about maintaining backwards compatibility. I suspect I’d just remembered it wrong (another sign I’m getting too old).
The older the wiser. + We remember the same thing.
I remember for a fact that findOne() caused issues with reactivity changing to find().fetch()[0] solved the issue. I had this problem in 1.2 others had it as well and I know I helped them with the exact same thing. Either that or I’ve been transported into an alternate universe where that has not been the case
Thanks @carina! I tried that and it works as a quick hack. I got around the problem by using two buttons but it’s helpful to know that this could also work! I’m having trouble getting the currentMap object to be reactive as I’m using it within multiple templates that are being displayed all at the same time but so far it’s all holding together.