如何在Meteor AutoForm提交上等待用户响应?

时间:2016-03-30 18:17:14

标签: javascript meteor meteor-autoform sweetalert

我正在使用Meteor和Aldeed的Autoform。我想在提交之前检查用户是否确定。我尝试了很多东西但是当我按下按钮时,表格仍会提交。这就是我现在拥有的东西,即使在背景中提交也是如此,它会很好地产生一个模态(使用SweetAlert):

AutoForm.hooks({
    createEventForm: {
        before: function() {
            this.event.preventDefault();
        },
        beginSubmit: function() {
            this.event.preventDefault();
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this imaginary file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete it!",
                closeOnConfirm: true },
                function(){
                swal("Deleted!", "Your imaginary file has been deleted.", "success"); });
        },

如何让表单等待用户确认或取消操作?

谢谢!

1 个答案:

答案 0 :(得分:1)

在表单提交的开头调用beginSubmit。作为documentation状态,它可用于在提交更长的请求时禁用/启用按钮或显示等待消息。如果您想显示确认消息并根据用户的决定提交表单,则需要使用before挂钩。

例如:

AutoForm.hooks({
  createEventForm: hooksObject
});

var hooksObject = {
  before: {
    insert: function(doc) {
      var self = this;
      swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        closeOnConfirm: true
      }, function(isConfirm) {
        if (isConfirm) {
          /* Submit form: */
          self.result(doc);
          swal("Deleted!", "Your imaginary file has been deleted.", "success");
        } else {
          /* Async cancel form submission: */
          self.result(false);
        }
      });
    }
  }
}