简体   繁体   English

登录时重新渲染模板

[英]Re-render template on login

In navbar I have 2 dropdowns, and one of them is available only if user is logged in (otherwise I hide it with {{#if currentUser}} ). 在navbar中,我有2个下拉菜单,其中只有在用户登录后才可用(否则我会用{{#if currentUser}}将其隐藏)。

For a dropdown to work it should be activated with jquery like this: $('.somedropdown').dropdown(); 为了使下拉菜单正常工作,应使用jquery这样激活它: $('.somedropdown').dropdown(); .

In meteor: 在流星中:

Template.navbar.rendered = function () { this.$('.somedropdown') .dropdown(); };

In my case I assigned these classes to dropdowns: .guest (for dropdown that is available all the time) and .useronly (for that which is available only if user is signed in). 在我的情况下,我将这些类分配给了下拉菜单: .guest (对于始终可用的下拉菜单)和.useronly (对于仅在用户登录.useronly可用的下拉菜单)。

And thus I try to do re-rendering of navbar adding this.autorun to rendered callback. 因此,我尝试重新渲染导航栏,将this.autorun添加到rendered回调中。 It checks if user is logged in and should trigger re-rendering accordingly: 它检查用户是否已登录,并应相应触发重新渲染:

Template.navbar.rendered = function () { this.$('.guest').dropdown(); this.autorun(function() { if(Meteor.userId()) { $('.useronly').dropdown(); } }); };

But it does not work. 但这行不通。

  1. When I first load webpage navbar renders OK without the .useronly dropdown. 首次加载网页时,Navbar会显示OK,而不会显示.useronly下拉列表。
  2. When I sign in the .useronly dropdown appears but it is not activated via jquery. 当我登录时,出现.useronly下拉列表,但未通过jquery激活。
  3. If I then run activation code $('.useronly').dropdown(); 如果然后运行激活代码$('.useronly').dropdown(); in console - the dropdown starts to work. 在控制台中-下拉列表开始工作。 It means the problem is not with jquery code. 这意味着问题不在于jQuery代码。

Meteor.userId() is reactive so I don't get it why this.autorun does not thigger re-rendering. Meteor.userId()是反应性的,所以我不明白为什么this.autorun不会重新渲染。

PS If it is important to mention, I use Semantic UI dropdown module http://semantic-ui.com/modules/dropdown.html (this is where jquery activation code comes from). PS:如果需要提及,我使用语义UI下拉模块http://semantic-ui.com/modules/dropdown.html (这是jquery激活代码的来源)。

PPS there are already similar questions on stackoverflow but I did not find any appropriate solution from the answers. PPS已经有关于stackoverflow的类似问题,但是我没有从答案中找到任何合适的解决方案。

Thank you for any help in advance. 感谢您的任何帮助。

SOLUTION

By adding console.logs into the code mentioned above I found that autorun actually worked but the jquery code ran before dropdown was actually rendered. 通过将console.logs添加到上述代码中,我发现自动运行确实有效,但是在实际呈现下拉菜单之前运行了jquery代码。 So I wrapped autorun function into Meteor.setTimeout(): 所以我将自动运行功能包装到Meteor.setTimeout()中:

Template.header.rendered = function () { // activate always available .guest dropdown this.$('.guest') .dropdown(); // activate .useronly dropdown if user is logged in this.autorun(function() { if(Meteor.userId()) { Meteor.setTimeout(function() { // add 0 delay just to add it last to the event loop $('.useronly') .dropdown(); }, 0); } }); };

PS also check the neater solution by https://stackoverflow.com/users/2104665/peppe-lg in answers. PS还会在答案中通过https://stackoverflow.com/users/2104665/peppe-lg检查更整洁的解决方案。

I'm guessing your autorun runs before the {{#if currentUser}} block in your template is updated, which means $('.useronly') won't refer to any element. 我猜想您的自动运行会在模板中的{{#if currentUser}}块更新之前运行,这意味着$('.useronly')不会引用任何元素。

I think the Meteor way to solve this is by using an extra template: 我认为流星解决此问题的方法是使用额外的模板:

<template name="main">
    {{#if currentUser}}
        {{> signedInDropDown}}
    {{/if}}
</template>

<template name="signedInDropDown">
    <!-- Your drop down here. -->
</template>
Template['signedInDropDown'].rendered = function(){
    this.$('.useronly').dropdown();
}

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

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