vendredi 2 janvier 2015

How to make the following template helper reactive (Meteor)?


I have Chapters collection, and I show a list of chapters in one of the templates:



<template name="chapterItem">
<div class="chapter clearfix {{isCurrentChapter}}">
<div class="chapter-arrows">
<a class="move-up" href="javascript:;"><i class="ion-arrow-up-a"></i></a>
<a class="move-down" href="javascript:;"><i class="ion-arrow-down-a"></i></a>
</div>
<h4><a class="open-chapter" href="javascript:;">{{title}}</a></h4>
<a class="delete-current-chapter" href="javascript:;"><i class="ion-close-circled"></i></a>
</div>
</template>


As you can see, I created a isCurrentChapter to use like this:



// book_page.js
Template.bookPage.events
"click .open-chapter": function() {
localStorage.setItem "currentChapter", this._id
}

// chapter_item.js
Template.chapterItem.helpers({
isCurrentChapter: function() {
var currentChapterId = localStorage.getItem("currentChapter");
var selectedChapterId = this._id;
if selectedChapterId === currentChapterId) {
return "active";
}
}
});


The problem now is that the active is returned only changes when the page loads.


How can I do it so that isCurrentChapter becomes reactive? Fires up on the click .open-chapter event?





Aucun commentaire:

Enregistrer un commentaire