如何为“插入”表单中的字段传递默认值?

我正在使用 Meteor 的软件包:Autoform、Collections2 和 Simple-Schema。

我的流程是:

  • 用户在页面上的列表中选择某个值,然后
  • 打开的“插入”,我希望用用户在上一步中选择的值初始化该字段。

  • 无法弄清楚如何通过 URL(或任何其他方式)传递参数。
    问题是如何用值初始化表单。

    假设我有一个 URL:
    http://localhost:3000/activity/new/Sport
    

    ================
    路由器.js:
    ...
    Router.map(function () {
        ...
        this.route('newActivity', {
            path: '/activity/new/:tag',
            data: function() {
                Session.set('tag', this.params.tag);
                return null;
            }
        });
        ...
    

    ================
    模型/activity.js
    ...
    Activities = new Meteor.Collection("activities", {
        schema: {
            title: {
                type: String,
                label: 'название'
            },
            ...
            tag: {
                type: String,
                label: 'тэг'
            }
        }
    });
    

    ================
    模板/avtibity.js
    ...
    Template.newActivity.helpers({
        defaultTag: function() {
            return Session.get('tag');
        }
    });
    ...
    

    ================
    模板/事件.html
    ...
    <template name="newActivity">
        <h1>Create new Activity!</h1>
        {{#autoForm collection="Activities" id="insertActivityForm" type="insert"}}
            {{> afQuickField name="title"}}
            ...
            {{> afQuickField name="tag" value="   ??????    "}} // ? {{defaultTag}}
            ho ho ho {{defaultTag}}
        {{/autoForm}}
    </template>
    

    ``

    最佳答案

    感谢 Eric Dobbertin:

  • 您可以将 value 设置为等于返回所需值的 helper ({{> afQuickField name="tag"value=valueHelper}})
  • 列表项 您可以将 doc 设置为一个对象,该对象的值设置为您想要的值。就像更新表单一样。

  • https://github.com/aldeed/meteor-autoform/issues/210

    关于meteor - 如何以 'insert' 形式传递字段的默认值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24187784/

    10-12 12:35
    查看更多