【问题标题】:Manipulating field content on client / server using autoform for meteor使用流星的自动表单在客户端/服务器上操作字段内容
【发布时间】:2015-04-21 20:29:40
【问题描述】:

我正在玩 Meteor 和 autoform。

我的设置

我有一个输入字段(输入“时间”)作为“歌曲”集合架构的一部分,它强制用户输入语法 [num][num]:[num][num] 。 这是我的架构:

time: {
    type: Number,
    label: "Time",
    optional: true,
    autoform: {
        afFieldInput: {
            type: 'time'
        }
    }
}

我想做什么

点击“提交”后但验证前我想转换字符串(例如“03:45”)秒(数字) 以便验证通过而不会出现错误。

另外:从数据库中读取数据时,我想将其转换回字符串,以便将其作为值放入输入字段中。

我在 autoform、collection2 或 simple-schema 的文档中找不到答案(或者至少不理解它 ;-)

感谢您的帮助!

【问题讨论】:

  • 我通过在 Sings 模式中定义 autoValue: function() {} 设法将字符串转换为秒。我还没有尝试从数据库返回表单的方式。另一件事是,我需要专辑的 ID 作为歌曲文档的属性。如果没有 autoform,我已经将它作为参数从模板助手传递给 Meteor.call(),但是使用 autoform 我不知道最好的方法。也许通过隐藏的输入字段?还是更花哨的方式?

标签: javascript meteor meteor-autoform


【解决方案1】:

使用自动生成钩子。您正在寻找的信息在这里: https://github.com/aldeed/meteor-autoform#callbackshooks

更具体地说,您正在寻找的是:

AutoForm.hooks({
  someFormId: {
    formToDoc: function(doc) {
        // Called every time an insert or typeless form
        // is revalidated, which can be often if keyup
        // validation is used.
    },
    docToForm: function(doc, ss) {
        // Called whenever `doc` attribute reactively changes, before values
        // are set in the form fields.
    }
});

可以在每个钩子中修改文档,特别是当它进入集合时以及从集合中被动地拉出文档时。

【讨论】:

  • 太好了,谢谢!钩子只能在客户端上使用吗?如果我想做某事怎么办。只在服务器上?我试图在服务器上调用AutoForm.hooks,但那里没有定义AutoForm。我说得对吗?连接服务器端的唯一方法是在 HTML 模板中传递一个meteormethod="someMeteorMethod",然后在那里施展魔法?
  • 钩子仅在客户端可用,因为 autoform 本身是仅客户端包。任何验证和清理都应该在你的模式中完成,而不是在钩子中,因为模式在客户端和服务器端都被调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 2015-12-10
  • 2016-09-01
  • 1970-01-01
  • 2015-03-18
相关资源
最近更新 更多