流星:为什么我的模板助手不是反应式的?

Meteor: Why my template helper isn't reactive?

我想获取路径以便为活动标签设置活动 class。如果我每次都单击 "refresh",代码就可以工作。

这是我的代码:

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>

完整回购

https://github.com/mvaisanen/flashcards

window 对象不是响应式的,因此模板不知道更改。尝试在它周围使用 Tracker.autorun 来产生反应性行为。

检查此 link,其中包含有关 Tracker 的更多信息

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

Iron.Location.get().path returns 实际路径并且是反应式的,所以我想你可以使用这个