0

朋友们,

我正在开发我在 Meteor 中的第一个应用程序,并在某些东西上撞到墙上......

我有一个类似于博客+评论情况的场景,其中我有一个集合(称为“帖子”)并且想要关联来自另一个集合的文档(称为“评论”)。

我知道将 post._id 作为“postId”字段传递给评论的最佳方法是使用 Flow Router 参数,因为表单位于“post/:id”视图上。

但是对于我的生活,我无法弄清楚如何获得“var postId = FlowRouter.getParam('postId');” 传递给 Autoform 以便填充。我尝试将它作为模式中的函数、钩子和页面上表单中的隐藏字段添加(显然不想走那条路线)。

Autoform 非常棒,我想使用它,但如果我无法获得这个该死的值来填充它,可能不得不将它连接起来。

有任何想法吗?这几天我一直在用头撞墙。

谢谢!

4

2 回答 2

2

首先,只要我们在同一页面上,如果您的路线设置如下:

FlowRouter.route('/blog/:postId', {
  action: function (params, queryParams) {
    FlowLayout.render('layout', { body: 'postTemplate' });
  },
});

您可以FlowRouter.getParam('postId')从 AutoForm 挂钩内部调用

您需要使用 AutoForm 挂钩拥有完整的架构。我正在使用该包aldeed:collection2来设置架构。该postId字段必须明确声明。此代码在服务器和客户端上运行。

Comments = new Mongo.Collection("comments");

Comments.attachSchema(new SimpleSchema({
  comment: {
    type: String,
    label: "Comment"
  },
  postId: {
    type: String
  }
}));

像这样设置表单不是您想要的:

{{> quickForm collection="Comments" id="commentForm" type="insert"}}

这不好,因为它会postId在 HTML 输出中显示该字段。我们不希望这样,因此您必须像这样完全定义表单:

{{#autoForm collection="Comments" id="commentForm" type="insert"}}
    <fieldset>
        {{> afQuickField name='comment' rows=6}}
    </fieldset>

    <button type="submit" class="btn btn-primary">Insert</button>
{{/autoForm}}

然后添加 AutoForm 钩子。此代码在客户端上运行。

var commentHooks = {
  before: {
    insert: function(doc){
      var postId = FlowRouter.getParam('postId');
      doc.postId = postId;
      return doc;
    }
  }
};

AutoForm.addHooks(['commentForm'],commentHooks);

确保您设置了允许/拒绝规则,并且它应该可以正常工作。

于 2016-01-11T01:03:23.403 回答
0

我也在同样的用例上苦苦挣扎,我在 Meteor 论坛上找到了这个:https ://forums.meteor.com/t/use-flow-router-param-in-autoform/14433/2

如果您正在使用模式来构建表单(使用 autoform 或 quickform 标签),那么您可以将它放在那里。

例如:

CampaignId:{类型:字符串,自动形式:{值:函数(){返回FlowRouter.getParam('campaignId');},输入:“隐藏” } },

于 2017-01-10T01:49:45.537 回答