这段代码不起作用(无缘无故)?

时间:2014-03-01 02:45:20

标签: javascript html hyperlink meteor account

此代码来自本教程:https://waaave.com/tutorial/meteor/design-a-complete-authentication-system-with-meteor/#L11

这是步骤6.1,据我所知,没有错误但由于某种原因它不起作用,当我点击它时“忘记密码”链接什么都不做。有谁知道什么是错的?

使用Javascript:

Template.main.helpers({
    showForgotPassword: function() {
        return Session.get('showForgotPassword');
    }
});

// Sign In Template
Template.signIn.events({
    'submit #signInForm': function(e, t) {
        e.preventDefault();

        var signInForm = $(e.currentTarget),
            email = trimInput(signInForm.find('.email').val().toLowerCase()),
            password = signInForm.find('.password').val();

        if (isNotEmpty(email) && isEmail(email) && isNotEmpty(password) && isValidPassword(password)) {
            Meteor.loginWithPassword(email, password, function(err) {
                if (err) {
                    Session.set('alert', 'We\'re sorry but these credentials are not valid.');
                } else {
                    Sesson.set('alert', 'Welcome back New Meteorite!');

                }
            });
        }
        return false;
    },

      'click #showForgotPassword': function(e, t) {
        Session.set('showForgotPassword', true);
        return false;
    },

});

HTML:

<div id="topbar">
            {{> signOut}}
     {{#unless resetPassword}}
         {{#unless currentUser}}
             {{#unless showForgotPassword}}
             {{else}}
                  {{> forgotPassword}}
             {{/unless}}
         {{else}}
          {{/unless}}
     {{else}}
         {{> resetPassword}}
     {{/unless}}
    </div>

<template name="signIn">
            <a class="show-forgot-password" href="javascript:void(0);" id="showForgotPassword">Forgot Password?</a>
    <div class="sign-in" id="signIn">
        <form action="/signin" class="sign-in-form" id="signInForm" method="post">
            <input class="email" id="signInEmail" type="text" name="email" placeholder="Email">
            <input class="password" id="signInPassword" type="password" name="password" placeholder="Password">
            <input class="btn btn-success" id"signInButton" type="submit" value="Sign In">
        </form>
    </div>
    <!-- end .sign-in -->
</template>

<template name="signOut">
    <div class="connected">
        <a class="sign-out" href="/" id="signOut">Sign Out</a>
    </div>
    <!-- end .connected -->
</template>

<template name="signUp">
    <div class="sign-up">
        <form action="/sign-up" class="sign-up-form" id="signUpForm" method="post">
            <input class="username" id="signUpUsername" name="username" placeholder="Username" type="text" >
            <input class="email" id="signUpEmail" name="email" placeholder="Email" type="text" >
            <input class="password" id="signUpPassword" name="password" placeholder="Password" type="password">
            <input class="password-confirm" id="signUpPasswordConfirm" name="password-confirm" placeholder="Confirm" type="password">
            <input class="btn btn-success" type="submit" id="submit" value="Sign up for Chattly">
        </form>
    </div>
    <!-- end .sign-up -->
</template>

<template name="forgotPassword">
    <div class="forgot-password" id="forgotPassword">
        <a class="return-to-sign-in" href="javascript:void(0);" id="returnToSignIn">Return</a>
        <form action="/forgot" class="forgot-password-form" id="forgotPasswordForm" method="post">
            <input class="email" id="forgotPasswordEmail" type="text" name="email" placeholder="Email Address">
            <input class="btn-submit" type="submit" value="Send">
        </form>
    </div>
    <!-- end .forgot-password -->
</template>

<template name="resetPassword">
    <div class="reset-password">
        <h2 class="headline">You're now saved, <span class="bold">follow the light!</span></h2>
        <h3 class="details">You just have to type and confirm your new password to regain access to your dashboard.</h3>
        <form action="/reset-password" class="reset-password-form" id="resetPasswordForm" method="post">
            <input class="password" id="resetPasswordPassword" name="password" placeholder="Your New Password" type="password" >
            <input class="password-confirm" id="resetPasswordPasswordConfirm" name="password-confirm" placeholder="Confirm New Password" type="password" >
            <input class="btn-submit" type="submit" value="Reset">
        </form>
    </div>
    <!-- end .reset-password -->
</template>

1 个答案:

答案 0 :(得分:1)

在本教程中的那一点(6.1),您还没有为forgotPassword配置任何事件处理程序。它没有用,因为代码还没有。

在下一节6.2中,作者将引导您发送一封用于启动密码恢复的电子邮件:

Template.forgotPassword.events({
    'submit #forgotPasswordForm': function(e, t) {
        e.preventDefault();

        var forgotPasswordForm = $(e.currentTarget),
            email = trimInput(forgotPasswordForm.find('#forgotPasswordEmail').val().toLowerCase());

        if (isNotEmpty(email) && isEmail(email)) {
            Accounts.forgotPassword({email: email}, function(err) {
                if (err) {
                    if (err.message === 'User not found [403]') {
                        Session.set('alert', 'This email does not exist.');
                    } else {
                        Session.set('alert', 'We\'re sorry but something went wrong.');
                    }
                } else {
                    Session.set('alert', 'Email Sent. Please check your mailbox to reset your password.');
                }
            });
        }
        return false;
    },

    'click #returnToSignIn': function(e, t) {
        Session.set('showForgotPassword', null);
        return false;
    },
});

这是我在最近的教程中看到的常见模式。

作者不是在开始时给你一个完整的应用程序并将其分解,而是采用迭代方法并逐个构建应用程序,这是你在构建应用程序时通常会做的事情。

因此它作为教程非常有效。请耐心等到最后:)

更新: 忘记密码表单没有显示,因为它应该在名为main的模板中,如下面的代码所示:

<template name="main">
    {{> alert}}
    {{#unless resetPassword}}
        {{#unless currentUser}}
            {{#unless showForgotPassword}}
                {{> signIn}}
                {{> signUp}}
            {{else}}
                {{> forgotPassword}}
            {{/unless}}
        {{else}}
            {{> signOut}}
        {{/unless}}
    {{else}}
        {{> resetPassword}}
    {{/unless}}
</template>

并且以下助手使用会话参数并将其提供给unless块:

Template.main.helpers({
    showForgotPassword: function() {
        return Session.get('showForgotPassword');
    }
});

但是您的代码没有main模板,而是将代码包装在通用div中,而且您似乎也更改了代码的其他部分。

<div id="topbar">
            {{> signOut}}
     {{#unless resetPassword}}
         {{#unless currentUser}}
             {{#unless showForgotPassword}}
             {{else}}
                  {{> forgotPassword}}
             {{/unless}}
         {{else}}
          {{/unless}}
     {{else}}
         {{> resetPassword}}
     {{/unless}}
    </div>

因此,您可以使用教程中的原始代码,或者如果您确实想要更改该代码,则可以使用原始代码中的topbar div包裹main template。< / p>