使用meteor-autoform和iron进行动态路由的问题:路由器

时间:2016-07-29 22:51:56

标签: javascript meteor iron-router meteor-autoform

我要做的是创建一个带有meteor-autoform的表单,该表单会在提交时将用户重定向到新生成的路由。我的思考过程是我可以提交_id并将其用于iron:router参数。到目前为止,我所看到的是:

创建表单

Submits = new Meteor.Collection('Submits');

Submits.allow({
  insert: function(username, doc){
    return !!username;
  }
});

SubmitSchema  = new SimpleSchema({
  title: {
    type: String,
    label: "Title"
  },
  subject:{
    type: String,
    label: "Subject"
  },
  summary:{
    type: String,
    label: "Summary"
  },
  author:{
    type: String,
    label: "Author",
    autoValue: function() {
      return this.userId
  },
  autoform: {
    type: "hidden"
  }
},
  createdAt: {
    type: Date,
    label: "Created At",
    autoValue: function(){
      return new Date()
    },
    autoform: {
      type: "hidden"
    }
  }
});

Submits.attachSchema( SubmitSchema );

路由

Router.route('/submit', {
  layoutTemplate: 'submitLayout',
  waitOn: function() { return Meteor.subscribe("Submits"); },
  loadingTemplate: 'loading'
});

Router.route('/submit/:_id', {
  name: 'formDisplay',
  data: function() {
    return Submits.findOne({this.params._id});
  }
});

然后我只有平均发布和查找电话。我的问题是我不确定如何在提交时执行重定向,我不知道如何在新生成的路由上显示表单结果。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我能够通过添加autoform.hook并稍微改变我的路由来实现。

Autoform hook:

AutoForm.addHooks('insertSubmit', {
  onSuccess: function(doc) {
    Router.go('formDisplay',{_id: this.docId});
  }
})

路由:

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

我从这篇文章中得到了这些信息:

Route to the new data submitted by Meteor autoform using iron router?