【问题标题】:Setting value for Omitted Field in Autoform in Meteor.jsMeteor.js 中 Autoform 中省略字段的设置值
【发布时间】:2015-03-13 01:23:10
【问题描述】:

当使用aldeed:autoform 包中的quickform 时,我们如何设置使用omitFields 省略的字段的值?该字段被省略,因为我们不希望用户更改其默认值(例如:从 Meteor.userId() 更改 userId)也看不到该字段。

例子:

{{> quickForm collection="Contacts" id="contacts-new-form" type="insert" omitFields="avatarUrl,details.active" buttonContent="Create Contact"}}

【问题讨论】:

    标签: javascript node.js meteor meteor-autoform


    【解决方案1】:

    如果您想省略字段,但为该字段提供默认值,则应使用 autovalue,如 https://github.com/aldeed/meteor-simple-schema#autovalue 中所述

    或者,您可以在架构定义中定义您的 autoform 属性,并将它们放在一起,如下所述:

    https://github.com/aldeed/meteor-autoform#putting-field-attribute-defaults-in-the-schema

    如果这是一个更新表单并且您不是在寻找自动值,而是保留原始值,那么您也可以将该特定字段设置为隐藏输入字段,如 https://github.com/aldeed/meteor-collection2#problems 所述,以便它仍然可以通过验证.

    但我会使用可靠的自动值定义。

    【讨论】:

    • 而我们究竟是如何访问一些模板数据,比如 autovalue 中的 avatarUrl?
    • 您无法从collection2 实例中访问模​​板数据,因为它运行在客户端 模板 可用的服务器上。因此,您需要访问数据的实际位置。如果 avatarURL 是用户信息的一部分,则直接访问用户。 Meteor.users.findOne(this.userId).profile.avatarUrl 或许对你有用!
    • 好的。为了添加其他客户端的东西,我们会使用钩子吗?喜欢下面的答案?
    • 您应该为此使用自动值。阅读 collection2 文档。
    【解决方案2】:

    @Nyxynyx 您需要添加此代码。只需在插入之前使用钩子并添加 userId 即可。看看下面的例子,希望对你有帮助。

    var postHooks = {
      before: {
        insert: function(doc) {
          if(Meteor.userId()){
            doc.userId = Meteor.userId();
          }
          
          return doc;
        }
      }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 2015-11-13
      • 2013-02-22
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多