【问题标题】:Meteor: Why my template helper isn't reactive?Meteor:为什么我的模板助手不是响应式的?
【发布时间】:2015-03-11 21:10:29
【问题描述】:

我想获取路径以便为活动选项卡设置活动类。如果我每次都点击“刷新”,代码就可以工作。

这是我的代码:

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

【问题讨论】:

  • Iron.current() 保存路径并且是响应式的,试试这个
  • 这是在创建 Meteor 应用程序时使用 Iron Router 的众多原因之一。除非您的应用程序不像单页应用程序那样运行很重要,尽管我认为这是一个非常罕见的边缘情况。 meteor add iron:routerHere are the docs

标签: javascript twitter-bootstrap meteor


【解决方案1】:

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

【讨论】:

  • 谢谢!这就像一个魅力。顺便说一句 Router.current().route.path(this); 也可能是一个可能的解决方案。
【解决方案2】:

窗口对象不是响应式的,则模板不知道更改。尝试在它周围使用 Tracker.autorun 以产生反应行为。

查看此链接,其中包含有关 Tracker 的更多信息

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

【讨论】:

    猜你喜欢
    • 2015-11-08
    • 2015-07-28
    • 2015-12-17
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 2016-01-16
    相关资源
    最近更新 更多