【问题标题】:Meteor: use dynamic parameters in a template callMeteor:在模板调用中使用动态参数
【发布时间】:2015-07-13 01:21:56
【问题描述】:

在我的 Meteor 应用程序中,我有 TAPi18n 包和 aldeed:autoform。我正在尝试使用 i18n 对占位符字段进行表单验证,但我不知道该怎么做。

所以,我想知道是否可以使用Spacebars 将动态参数(我这样称呼它)传递给模板。我的意思是,在 JS 文件中不使用帮助程序,如下所示:

<template name="Publish">
    <div class="col-md-8 col-lg-8 col-md-offset-2 col-lg-offset-2 well">
        {{#autoForm collection="Publications" id="insertPublicationForm" type="insert"}}
            <fieldset>
                <legend>{{_ "publish_title"}}</legend>

                <div  class="col-md-12 col-lg-12">
                    <div  class="form-group">
                        {{> afFieldInput name='name' placeholder={{_ "name"}} }}
                    </div>

                    <div  class="form-group">
                        {{> afFieldInput name='description' placeholder={{_ "description"}} }}
                    </div>

                    <div  class="form-group">
                        {{> afFieldInput name='price' placeholder={{_ "price"}} }}
                    </div>
                </div>

                <div  class="form-group">
                    <button type="submit" id="add_publication" class="btn btn-success center-block">{{_ "publish"}}</button>
                </div>
            </fieldset>
        {{/autoForm}}
    </div>
</template>

我可以为每个翻译注册一个助手,但我不太喜欢这个想法。

我也知道我可以使用SimpleSchema 中的标签字段,如下所示:

Meteor.startup(function() {
    Publications.attachSchema(new SimpleSchema({
        name: {
            type: String,
            label: TAPi18n.__("name"),
            max: 200
        },
        description: {
            type: String,
            label: TAPi18n.__("description")
        },
        price: {
            type: Number,
            label: TAPi18n.__("price"),
            min: 0
        }
    }));
});

然后使用afQuickField 模板代替afFieldInput。 但我不想使用标签,我想使用输入的占位符。

有什么办法吗?

【问题讨论】:

    标签: validation templates meteor internationalization spacebars


    【解决方案1】:

    好吧,我不知道为什么我以前没有看到它,但我可以在SimpleSchema 中做到这一点:

    Meteor.startup(function() {
        Publications.attachSchema(new SimpleSchema({
            name: {
                type: String,
                label: TAPi18n.__("name"),
                max: 200,
                autoform: {
                    afFieldInput: {
                        placeholder: TAPi18n.__("name")
                    }
                }
            },
            description: {
                type: String,
                autoform: {
                    afFieldInput: {
                        placeholder: TAPi18n.__("description")
                    }
                }
            },
            price: {
                type: Number,
                min: 0,
                autoform: {
                    afFieldInput: {
                        placeholder: TAPi18n.__("price")
                    }
                }
            }
        }));
    });
    

    这样我就可以在占位符中使用 i18n 而无需制作大量的助手。

    对不起,如果我让别人在这上面浪费时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 2013-07-24
      • 2015-12-09
      • 2015-07-10
      • 1970-01-01
      相关资源
      最近更新 更多