简体   繁体   English

强制流星刷新/重新渲染模板?

[英]Force Meteor To Refresh / Re-render Templates?

*For reference I'm using iron router. *作为参考,我使用的是铁路由器。

Instead of a sign in page I have this global sign in form embedded in an nav (aka on every page). 我将这个全局登录表单而不是登录页面嵌入到导航栏中(也称为每个页面)。 Right now I'm doing a really hacky refresh to reload the page once a user logs in. 现在,我正在进行一次非常hacky的刷新,以在用户登录后重新加载页面。

I would like to just reload to the template aka not refresh the whole page. 我只想重新加载到模板,也就是不刷新整个页面。 Basically just want the templates rendered function to rerun on login. 基本上只希望模板呈现的功能在登录时重新运行。

Here's my current login code: 这是我当前的登录代码:

'submit #login': function(event, template){
    event.preventDefault();
    var handle = template.find('#usernameLogin').value;
    var secretKey = template.find('#passwordLogin').value;
    Meteor.loginWithPassword(handle, secretKey, function(err){
        if (err) {
            alert(err);
        }else{
            $('#close').click();
            /* replace this with reactive ajax or whatever when you can! */
            Meteor._reload.reload();
        }
    });
},

My render function which I think may be the real issue now: 我认为现在的渲染功能可能是真正的问题:

Template.tournament.rendered = function () {
    thisCampaign = this.data;
    var self = this;

    if (this.data.tournament.live) {

        /* if theres a registered user */
        if (Meteor.userId()) {

            /* Select a winner box */
            var participants = $('.participant-id');
            var currentParticipant;
            var nextRound;
            var thisMatch;
            var nextMatch;
            var bracket;
            participants.map(function(index, value){
                if ($(value).text() === Meteor.userId()) {
                    if ($(value).parent().find('.participant-status').text() === 'undetermined') {
                        nextRound = $(value).parent().find('.participant-round').text();
                        thisMatch = $(value).parent().find('.participant-match').text();
                        bracket = $(value).parent().parent().parent().find('.participant');
                    };
                };
            });
            nextRound = parseInt(nextRound) + 1;
            nextMatch = Math.round(parseInt(thisMatch)/2) - 1;
            if (parseInt(thisMatch) % 2 != 0) {
                currentParticipant = 0;
            }else{
                currentParticipant = 1;
            }
            var winnerOptions = '';
            var winnerBox = $('<div class="select-winner">');
            if (bracket) {
                bracket.map(function(index, value) {
                    winnerOptions += '<span class="winner-option"> '+$(value).find('.participant-title').text()+' <div class="winner-info"> '+$(value).find('a').html()+' </div> </span>'
                });
                winnerBox.append(winnerOptions);
                $($($('.round'+nextRound).find('li')[nextMatch]).find('.participant')[currentParticipant]).removeClass('loser').addClass('undetermined');
                $($($('.round'+nextRound).find('li')[nextMatch]).find('.participant')[currentParticipant]).find('a').addClass('tooltip').html(winnerBox);
            };

        }else{

        }

    }else{
        /* Tournament Start Time */

        var tournamentStartTime = function(){
            var d = new Date();
            var n = d.getTime();
            var currentTime = TimeSync.serverTime(n);
            var startTime = self.data.card.startTime;
            var difference = startTime - currentTime;
            var hoursDifference = Math.floor(difference/1000/60/60);
            difference -= hoursDifference*1000*60*60
            var minutesDifference = Math.floor(difference/1000/60);
            difference -= minutesDifference*1000*60
            var secondsDifference = Math.floor(difference/1000);
            /* if ends (make tournament live server side?) */
            if (hoursDifference < 0 || minutesDifference < 0 || secondsDifference < 0) {
                Meteor.clearInterval(tStartTime);
                Session.set("tournamentStartTime", false);
            }else{
                if (hoursDifference   < 10) {hoursDifference   = "0"+hoursDifference;}
                if (minutesDifference < 10) {minutesDifference = "0"+minutesDifference;}
                if (secondsDifference < 10) {secondsDifference = "0"+secondsDifference;}
                var formattedTime = hoursDifference + ':' + minutesDifference + ':' + secondsDifference;
                Session.set("tournamentStartTime", formattedTime);
            }
        };
        Session.set("tournamentStartTime", '00:00:00');
        tournamentStartTime();
        var tStartTime = Meteor.setInterval(tournamentStartTime, 1000);

        /* Allow new user sign up */
        var alreadySignedUp = false;
        var usersSignedUp = $('.participant-id')
        usersSignedUp.map(function (index, user) {
            if ($(user).text().trim() === Meteor.userId()) {
                alreadySignedUp = true;
            }
        });

        if (this.data.card.host != Meteor.user().username && !(alreadySignedUp)) {
            var openSlots = [];
            var allSlots = $('.participant');
            allSlots.map(function (index, participant) {
                if ($(participant).find('.participant-title').text().trim() === '' && !($(participant).hasClass('loser'))) {
                    openSlots.push(participant);
                }
            });
            openSlots.map(function (openSlot, index) {
                $(openSlot).removeClass('winner').addClass('undetermined');
            });
        }

        /* if theres a registered user */
        if (Meteor.userId()) {

        }else{

        }

    }
};

From what i can see there, your rendered function would not work as you expect as the template may render while the loggingIn state is still occuring... 从我可以看到的地方,您的渲染功能将无法按预期工作,因为在loggingIn状态仍在发生时模板可能会渲染...

My suggestion would be to use something along the lines of {{#if currentUser}} page here{{/if}} and then put the code you are trying to run in the rendered in a helper inside that currentUser block that way it would only display and be called if there is a logged in user, otherwise it would not show up and you would not need to re-render the page to perform any of that. 我的建议是在{{#if currentUser}} page here{{/if}}使用类似的内容,然后将要尝试运行的代码放在currentUser块内的帮助器中的呈现器中。仅在有登录用户时显示并被调用,否则它将不会显示,并且您无需重新渲染页面即可执行任何操作。

Basically once the user has logged in, any helper (other than rendered) that has the Meteor.userId() or Meteor.user() functions being called would re-run automatically, otherwise you could perform login actions inside a Tracker.autorun function if they are global to your app per client. 基本上,一旦用户登录,具有Meteor.userId()Meteor.user()函数的任何帮助程序(呈现的助手Meteor.user()都将自动重新运行,否则,您可以在Tracker.autorun函数中执行登录操作如果它们是每个客户对您的应用程序全局的。

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

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