【问题标题】:How to display success message using autoform in meteor?如何在流星中使用 autoform 显示成功消息?
【发布时间】:2014-12-05 04:01:33
【问题描述】:

我正在使用https://github.com/aldeed/meteor-autoform 收集我的一个流星。我正在使用 quickForm 并输入 insert。以下是相关代码:

<template name="postInsert">
   <legend>Add your story here</legend>
   {{> quickForm collection="Posts" id="insertPostForm" type="insert" buttonContent="Post!" resetOnSuccess=true}}
</template>

此表单提交并成功创建帖子。但它不显示成功消息。我知道我可以使用 onSuccess 挂钩并编写自己的成功消息。但我想知道是否有使用自动配置显示成功消息的标准方法?

我查看了 github 上的文档并进行了一些搜索,但所有解决方案都指向使用 onSuccess 钩子。这里的任何指针表示赞赏

【问题讨论】:

    标签: meteor


    【解决方案1】:

    经过大量搜索,事实证明,onSuccess 挂钩是显示成功消息的标准方式。为了完整起见,这是我为将来可能会偶然发现此问题的任何其他人执行的相同操作。

    新的 Autoform 6.0.0

    onSuccess: function(formType, result) {
        FlashMessages.sendSuccess('Success!');
        Router.go("/posts");
    },
    

    AutoForm.addHooks(['postInsert', 'postUpdate'], {
      onSuccess: function(operation, result, template) {
        FlashMessages.sendSuccess('Success!');
        Router.go("/posts");
      }
    });
    

    AutoForm.addHooks 的使用使代码保持干燥,允许重复使用更新和插入操作。

    我还使用出色的flash-messages 来显示我所有的用户消息。强烈推荐。

    【讨论】:

    • 感谢您回答问题(y)
    • 这段代码在哪里?我似乎无法在客户端运行任何东西。另外,postInsert 是您的模板名称吗?谢谢
    • 更新:代码在客户端运行,是的,传递给addHooks 的数组包含表单所在模板的名称。 :D 原来我使用了错误的模板名称(文档不清楚)
    【解决方案2】:

    我没有足够的声誉来发表评论,但根据documentation,似乎 Autoform.addHooks 现在采用了 formId。

    所以你可以使用 id 'insertPostForm' 绑定你的自动表单

    AutoForm.addHooks(['insertPostForm'], {
         onSuccess: function (operation, result, template) {
                  ...
         }
    });
    

    【讨论】:

      【解决方案3】:

      根据 github 上的文档,onSuccess hook

      的签名已更改
      AutoForm.addHooks(['yourForm'],{
          onSuccess: function(formType, result) {
              Router.go('page',{_id: this.docId});
          }
      });
      

      最好检查最新的签名:https://github.com/aldeed/meteor-autoform#callbackshooks

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        • 1970-01-01
        • 2011-08-07
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        • 2019-11-12
        相关资源
        最近更新 更多