【问题标题】:AutoForm fields自动表单字段
【发布时间】:2015-05-19 21:56:56
【问题描述】:

我正在使用包 aldeed:autoform 创建一个表单

这就是我的代码

CompanyData = new Mongo.Collection('companyData');
CompanyData.attachSchema(new SimpleSchema({
    allFields: {
        type: String,
        allowedValues: ['title', 'logo', 'url'],
        autoform: {
            type: "selectize"
        }
    },
    title:{
        type:'String',
        label:'Name',
        unique:true,
        max:100
    },

    logo:{
        type:'String',
        label:'Logo',
        optional:true
    }
}));

这就是我需要的

  1. 当用户在集合中插入数据时,我想添加一个名为“createdBy”的字段,其值为 userId。

  2. 当用户更新数据时,我想添加一个名为“updatedBy”的字段,其值为 userId。

现在,当用户更新数据时,'createdBy' 字段不应更新。但是应该更新“updatedBy”字段。

是的,当显示表单字段时,createdBy 和 updatedBy 字段将不显示。

任何帮助

【问题讨论】:

    标签: javascript meteor meteor-autoform


    【解决方案1】:

    meteor-collection2 自述文件 (https://github.com/aldeed/meteor-collection2#autovalue) 中有明确的文档。

     // Force value to be current date (on server) upon insert
      // and prevent updates thereafter.
      createdAt: {
        type: Date,
        autoValue: function() {
          if (this.isInsert) {
            return new Date;
          } else if (this.isUpsert) {
            return {$setOnInsert: new Date};
          } else {
            this.unset();
          }
        }
      },
      // Force value to be current date (on server) upon update
      // and don't allow it to be set upon insert.
      updatedAt: {
        type: Date,
        autoValue: function() {
          if (this.isUpdate) {
            return new Date();
          }
        },
        denyInsert: true,
        optional: true
      }
    

    在文档中的这个示例中,使用了 createdAt 和 updatedAt。您只需更改这些以引用用户 ID。

      createdBy: {
        type: String,
        autoValue: function() {
          if (this.isInsert) {
            return this.userId;
          } else if (this.isUpsert) {
            return {$setOnInsert: this.userId};
          } else {
            this.unset();
          }
        }
      },
      updatedBy: {
        type: String,
        autoValue: function() {
          if (this.isUpdate) {
            return this.userId;
          }
        },
        denyInsert: true,
        optional: true
      }
    

    您可以将其添加到每个字段中,以防止它出现在自动表单/快速表单中:

    autoform: {
      omit: true
    }
    

    或者您可以在表单中使用omitFields="createdBy,updatedBy"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 2018-08-28
      • 2011-05-23
      • 2020-11-01
      • 2019-07-18
      • 2016-06-11
      • 2016-02-14
      相关资源
      最近更新 更多