javascript - 自动表单字段

标签 javascript meteor meteor-autoform

我正在使用 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 字段不会显示。

任何帮助

最佳答案

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"

关于javascript - 自动表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325369/

相关文章:

javascript - 为什么我的 cookie 不将值写入输入(javascript)?

javascript - 用于验证逗号分隔年份范围的正则表达式

javascript - 在 jsfiddle 上看不到代码,只有结果

javascript - 如何在 Vue.js 中将 InnerHtml 复制到剪贴板?

javascript - meteor 手写笔预处理器添加 `j-` 前置到每个文本布局 :center

javascript - AutoForm-访问模板内对象属性数组的值

javascript - Meteor Autoform - 禁用对象字段数组中的选择选项

javascript - 当 DOM 发生变化时如何重新运行 JavaScript?

javascript - 如何重命名 Meteor 包?

javascript - 在 meteor 中实现自动表单模式