2016-02-25 66 views
0

我正在使用meteor.js中的Jquery.validate()包來確保將正確的輸入放入表單中。更改基於jquery.validate()的reactiveVar結果meteor.js

提交時,我使用reactiveVar來更改我的視圖輸出。如果通過驗證測試,我如何才能更改reactiveVar?

是這樣的:

thissite.js:

Template.inquiry.onCreated(function() { 
    //toggles 'thank you' on inquiry template 
    this.showForm = new ReactiveVar(true); 
}); 

Template.inquiry.onRendered(function(){ 
    $('#request-form').validate(); 
}); 

inquiry.js

Template.inquiry.helpers({ 

    //toggles 'thank you' on inquiry template 
    showForm: function() { 
    return Template.instance().showForm.get(); 
    }, 

}); 

Template.inquiry.events({ 
    'click .submit': function(event, template) { 
    template.showForm.set(false); 
    Meteor.setTimeout(function() { 
     template.showForm.set(true); 
    }, 10000); // Reset after 10 seconds. 
    }, 
}); 

inquiry.html:

<template name="inquiry"> 

{{#if showForm }} 

<form class="request-form" id="request-form" method="post" action="#"> 
<input type="text" name="name" id="name" value="" placeholder="Name" required /> 
<input type="email" name="email" id="email" value="" placeholder="Email" required /> 
<input class="submit" type="submit" value="Send Inquiry" /> 

{{else}} 

Thank You message 

{{/if}} 

</template> 

回答

1

更改您的代碼如下:

'click .submit': function(event, template) { 

     var $form = template.$('.request-form'); 

     if ($form.valid()) { 
      template.showForm.set(false); 

      Meteor.setTimeout(function() { 
       template.showForm.set(true); 
      }, 10000); // Reset after 10 seconds. 
     } 
    }, 
0

Jquery.validate()有回調的選擇方法submitHandler,你可以在validate plugin docs讀到它。

Template.inquiry.onRendered(function(){ 
    $('#request-form').validate({ 
     submitHandler: function() {...} 
    }); 
});