简体   繁体   English

把手帮手的流星渲染

[英]Meteor Force Rerender of Handlebar Helpers

In Meteor, I have a navigation bar with a custom handlebar method that determines whether or not the tab is active. 在Meteor中,我有一个带有自定义车把方法的导航栏,该方法确定选项卡是否处于活动状态。

JS: JS:

Handlebars.registerHelper('getClass',function(a){
    url = document.URL.split("/"); //e.g. http://test.com/something, so 3rd value will have link thing
    url = "/" + url[3];
    val = Router.path(a);
    return url == val ? "active" : "";
});

HTML Snippet: HTML片段:

  <li class="{{getClass 'homepage'}}"><a href="{{pathFor 'homepage'}}">Home</a></li>
  <li class="{{getClass 'content'}}"><a href="{{pathFor 'content'}}">Content</a></li>
  <li class="{{getClass 'items'}}"><a href="{{pathFor 'items'}}">Items</a></li>

This sets the appropriate active class on a nav-block if I open an entirely new tab, navigating to something like http://localhost:3000/content . 如果我打开一个全新的选项卡,导航到类似http://localhost:3000/content这将在导航块上设置适当的active类。 However, it won't update the active class if I merely click a new tab. 但是,如果我仅单击一个新选项卡,它将不会更新active类。 For instance, from the home page to the content page. 例如,从主页到content页面。 However, if I click the items tab and then click another tab, it will update, but just once. 但是,如果我单击items选项卡,然后单击另一个选项卡,它将更新,但只会更新一次。 So if I go to the items tab, items will have the active class and the next class I click will then get it, removing it from items . 因此,如果我转到项目标签,则items将具有活动的班级,而我单击的下一个班级将获得它,并将其从items删除。 This only works once, unfortunately. 不幸的是,这只能运行一次。 I think it is because items has content from a DB, but I'm not sure. 我认为这是因为items具有数据库中的内容,但是我不确定。 Anyways, I need a way to rerender handlebar helpers, if this is at all possible. 无论如何,如果可能的话,我需要一种重新渲染车把帮手的方法。

I've tried using the following JS to no avail: 我尝试使用以下JS无效:

Content = {};
Content._dep = new Deps.Dependency;

Template.header.events({
  'click a': function () {
      console.log('abc');
    Content._dep.changed();
  }  
})

Does anybody have a way to rerender the handlebars without having to physically reload the page? 是否有人无需重新加载页面即可重新渲染车把?

Thanks. 谢谢。

I just fixed it. 我刚刚修好了。 Basically, I added a session with a random number, and updated that on click. 基本上,我添加了一个具有随机数的会话,并在点击后对其进行了更新。 It seemed to have done the trick. 看来已经成功了。

Updated code: 更新的代码:

Handlebars.registerHelper('getClass',function(a){
    url = document.URL.split("/"); //e.g. http://test.com/something, so 3rd value will have link thing
    url = "/" + url[3];
    val = Router.path(a);
    if(Session.get('r'))
        void(0);
    return url == val ? "active" : "";
});


Template.header.events({
  'click a': function () {
      Session.set('r', Math.random() + "");
  }  
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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