简体   繁体   中英

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

Iron.Location.get().path返回实际路径并且是反应性的,所以我猜你可以使用它

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM