您可以在成功创建帐户时将其隐藏,而不是立即隐藏 div。我做过类似的事情,它仍在进行中,希望对您有所帮助
我的注册模板:
<template name="SignUpForm">
<div id="signUpForm" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Sign Up</h3>
</div>
<form><div class="modal-body" align="center">
<input type="text" id="usernameSU" placeholder="Username" /><br />
<input type="password" id="passwordSU" placeholder="Password" /><br />
<input type="password" id="passconf" placeholder="Password" /><br />
<input type="text" id="email" placeholder="You@Email.com" /><br />
{{> CurErr}}
</div>
<div class="modal-footer">
<a href="#" id="btnSignUp" class="btn btn-primary">Sign Up</a>
</div></form>
</div>
</template>
CurErr 模板仅在引导警报中显示 curErr Session 变量中的任何内容。我的注册模板.js:
Template.SignUpForm.events({
'click #btnSignUp': function(e,t){
signUpUser();
}
});
function signUpUser(){
var password = $('#passwordSU').val();
var passconf = $('#passconf').val();
if (password !== passconf){
Session.set('curErr', 'Password fields do not match');
} else {
var options = {
username: $('#usernameSU').val(),
password: $('#passwordSU').val(),
email: $('#email').val(),
profile: {}
}
try{
//made a validation library, use your own validation logic here
validate(options.username, 'username');
validate(options.password, 'password');
validate(options.email, 'email');
Accounts.createUser(options, function(err){
if(err){
Session.set('curErr', err.reason);
}
else
$('#signUpForm').modal('hide');
});
}catch(err){
Session.set('curErr', err.reason);
}
}
}
所以我的注册模式会在成功创建用户时隐藏,否则我会在表单上显示错误并且用户不会丢失任何数据,如果表单关闭,数据在重新打开时仍会保留。