【问题标题】:Route to the new data submitted by Meteor autoform using iron router?使用 Iron Router 路由到 Meteor autoform 提交的新数据?
【发布时间】:2015-07-06 09:07:40
【问题描述】:

我将 Meteor 与 AutoForm 和 Iron Router 一起使用。

我有一个用于插入数据的自动表单,我想在插入成功后重定向到我添加的数据的页面。我该怎么做?

这是为了:

{{#autoForm collection="Products" id="add" type="insert"}}
    <h4 class="ui dividing header">Products Information</h4>
      {{> afQuickField name='name'}}
      {{> afQuickField name='info'}}
    <button type="submit" class="ui button">Insert</button>
{{/autoForm}}

铁路由器:

Router.route('/products/:_id', {
  name: 'page',
  data: function() { return Products.findOne(this.params._id);}
});

回调/挂钩

AutoForm.hooks({
  add: {
    onSuccess: function(doc) {
      Router.go('page', ???);
    }
  }
});

【问题讨论】:

标签: meteor iron-router meteor-autoform


【解决方案1】:

AutoForm 挂钩将返回您的 docId。看: https://github.com/aldeed/meteor-autoform#callbackshooks

this.docId:附加到表单的文档的 _id 属性,如果 有一个,或者对于 type='insert' 形式,新的 _id 插入的文档,如果已插入。

所以使用:

Router.go('page',{_id: this.docId});

【讨论】:

    【解决方案2】:

    根据 github 上的文档,签名发生了变化: 不要忘记声明表单或 null 以应用挂钩。

    适用于所有形式

    AutoForm.addHooks(null,{
        onSuccess: function(formType, result) {
            Router.go('page',{_id: this.docId});
        }
    });
    

    具体形式

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

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

    【讨论】:

      【解决方案3】:
      onSuccess: function(formType, result) {
          Router.go(
              ['adminDashboard', result, 'Edit'].join(''), 
              {_id: this.docId}
          );
      },
      

      【讨论】:

      • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      猜你喜欢
      • 2017-05-21
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多