【问题标题】:insert new doc via autoform hooks call meteor method通过 autoform 钩子调用流星方法插入新文档
【发布时间】:2016-04-17 20:43:52
【问题描述】:

我想使用 autoform 将一个新文档插入到 db 中。 Autoform 钩子调用服务器上的流星方法来插入文档。

我在模板中有这个...

{{#autoForm collection="Reports" id="addReport" type="insert"}}
    <div class="row">
        <div class="col s6">
            {{> afQuickField name='hours'}}
        </div>
    </div>
    <button class="btn waves-effect waves-light modal-action modal-close"><i class="material-icons">save</i></button>
{{/autoForm}}

那么……

AutoForm.hooks({
    addReport: {
        onSubmit: function(insertDoc) {
            Meteor.call('addReport', insertDoc, function(error, result) {
                if (error) alert(error.reason);
            });
            return false;
        }
    }
});

那么服务器上的方法...

Meteor.methods({
    addReport: function(insertDoc) {   
        var report = _.extend(insertDoc, {
            userId: Meteor.userId(),
        });
        return Reports.insert(report);
    }
});

我有一个 createdAtupdatedAt 字段在集合中,但它们都有 autoValue 因此,我相信不需要从客户端或在流星方法中进行插入。

所以带有架构的集合看起来像这样:

Reports = new Meteor.Collection('reports');

Reports.attachSchema(new SimpleSchema({
    hours: {
        type: Number,
        label: "Number of hours",
        decimal: true
    },
    createdAt: {
        type: Date,
        label: "Created Date",
        autoValue: function() {
            if (this.isInsert) {
                return new Date;
            } else {
                this.unset();
            }
        },
        denyUpdate: true
    },
    updatedAt: {
        type: Date,
        autoValue: function() {
            if (this.isUpdate) {
                return new Date()
            }
        },
        denyInsert: true,
        optional: true
    },
    "userId": {
        type: String,
        autoform: {
            type: "hidden",
        }
    },
}));

当我运行流星时,表单显示,但提交什么也不做。没有关于是否有任何错误的视觉提示。客户端和服务器控制台中都没有错误消息。

我做错了什么或错过了什么?

【问题讨论】:

  • 这是模态的吗?它在模态之外工作吗?
  • @AutumnLeonard 不。它不是模态的。
  • 嗯。我会在此过程中添加一些控制台日志,以查看正确调用的内容。
  • 我在方法调用中添加了一个控制台日志,以了解它何时被触发。它没有。哦,还有那个模态类,它关闭由其他东西引起的模态动作。
  • 您是否使用“保存”按钮提交?尝试添加type="submit"

标签: meteor meteor-autoform


【解决方案1】:

既然@Cristo GQ 是对的,我只是想确保答案对于该主题的未来访问者来说足够清楚


onSubmit hook用于 带有 type='normal'不带任何 @ 的 autoForms 987654324@一点都没有


另一方面,before.insert hook 仅适用于 type='insert' 并且没有 before.normal hook
这意味着当使用onSubmit hook 时,我们必须在onSubmit 本身内部做任何“工作前”(比如将 currentUser 添加到文档中)。

【讨论】:

    【解决方案2】:

    aldeed/meteor-autoform 文档:

      // Called when form does not have a `type` attribute
      onSubmit: function(insertDoc, updateDoc, currentDoc) {
         Meteor.call()...
      }
    

    我正在关注discovermeteor书,我正在尝试使用书中的一些方法,但使用meteor-autoform包。

    post_submit.html

    <template name="postSubmit">
    
        {{#autoForm collection="Posts" id="insertPost"}} <-- no type
    
                <div class="form-group">
                    <div class="controls">
                        {{> afQuickField name='title' class='title form-control'}}
                    </div>
                </div>
                <div class="form-group">
                    <div class="controls">
                        {{> afQuickField name='description' class='description form-control'}}
                    </div>
                </div>
    
                <input id="send" type="submit" value="Send" class="btn btn-primary"/>
    
        {{/autoForm}}
    
    </template>
    

    post_submit.js

    var postSubmitHook = {
    
        onSubmit: function(insertDoc){
            Meteor.call('postInsert', insertDoc, function(error, result) {
    
                if (error){
                    Bert.alert(error.reason, 'danger', 'growl-top-right');
                    $('#send').removeAttr('disabled');
                    return;
                }
    
                Router.go('postPage', {_id: result._id});
            });
            return false;
        }
    };
    
    AutoForm.addHooks('insertPost', postSubmitHook);
    

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多