Mika
Mika

Reputation: 1152

Meteor: Why my template helper isn't reactive?

I want to get path in order to set active class for active tabs. The code works, if I click "refresh" everytime.

Here is my code:

JS

Template.header.helpers({
    isCurrentPage: function(pageName){
        var pathArr = window.location.pathname.split("/");
        var firstLevelPath = pathArr[1];
        return firstLevelPath == pageName
    }
});

HTML

<li role="presentation" class="{{#if isCurrentPage 'browse'}}active{{/if}}"><a href="{{pathFor 'browse'}}">Browse</a></li>

Full repo

https://github.com/mvaisanen/flashcards

Upvotes: 1

Views: 147

Answers (2)

sdooo
sdooo

Reputation: 1881

Iron.Location.get().path returns actual path and is reactive, so I guess you could use this

Upvotes: 1

Filipe N&#233;vola
Filipe N&#233;vola

Reputation: 63

The window object is not reactive then the template does not know about the change. Try to use Tracker.autorun around it to have a reactive behavior.

Check this link that contains more about Tracker

http://docs.meteor.com/#/full/tracker_autorun

Upvotes: 0

Related Questions