2014-09-26 134 views

回答

2

下面是一些样板代码,你可以玩的:

HTML

<template name="myForm"> 
    <form> 
    <button type="submit" disabled={{loading}}> 
     {{! display different text based on a reactive helper}} 
     {{#if loading}} 
     Signing in... 
     {{else}} 
     Sign in 
     {{/if}} 
    </button> 
    </form> 
</template> 

JS

Template.myForm.created=function(){ 
    // attach a reactive var to the template instance 
    // you need to meteor add reactive-var first ! 
    this.loading=new ReactiveVar(false); 
}; 

Template.myForm.helpers({ 
    loading:function(){ 
    // return the value of the reactive var attached to this template instance 
    return Template.instance().loading.get(); 
    } 
}); 

Template.myForm.events({ 
    "submit":function(event,template){ 
    // mandatory to prevent page refresh inherent to classic from submission 
    event.preventDefault(); 
    // set loading var to true 
    template.loading.set(true); 
    // we use Meteor.loginWithPassword as an example, but you could use 
    // any method call to the server here 
    Meteor.loginWithPassword(...,function(error){ 
     // reset the reactive var even if the login failed (to allow retries) 
     template.loading.set(false); 
     // 
     if(error){ 
     console.log(error); 
     return; 
     } 
     // success code goes here 
    }); 
    } 
}); 

编辑:为按钮免费添加禁用状态。

+0

提示:使用'禁用=“{{负载}}”'而不是'{{禁用}}'。 – 2014-09-26 15:35:38

+0

没有意识到这一点,感谢亲的技巧:) https://github.com/meteor/meteor/wiki/Using-Blaze#conditional-attributes-with-no-value-eg-checked-selected – saimeunt 2014-09-26 16:03:05

0

你可以改变你的按钮对登录的单击事件文本,例如:

$("#yourbuttonid").click(function() { 
    $("#yourbuttonid").val("Signing in"); 
});