如何使用 simpleSchema 创建可重用组件

How to create reusable component with simpleSchema

我的想法可能是错误的,所以请随时纠正我的想法。

我正在使用 simpleSchema,并且有一段代码用于多个模式。有没有一种方法可以创建一个单独的组件并将其导入到每个模式中,这样当我需要更新组件时我就不必在多个位置更新它?

路径:resuableComponent

type: String,
  optional: true,
  autoform: {
    type: "select",
    options: function () {
      return [
        {label: "School logo 1", value: 'url'},
        {label: "School logo 2", value: 'url'},
        {label: "School logo 3", value: 'url'},
      ];
    },
  }

路径:studentCollection.js

Schemas.Student = new SimpleSchema({
    studentUserId: {
        type: String,
    },
    school: {
        type: String,
        optional: false
    },
    **resuableComponent**
});

路径:teacherCollection.js

Schemas.Teacher = new SimpleSchema({
    teacherUserId: {
        type: String,
    },
    school: {
        type: String,
        optional: false
    },
    **resuableComponent**
});

如果您使用的是 SimpleSchema,您可以将可重用对象移动到一个在客户端和服务器上都应该可见的不同文件中。

基于您的问题的示例:

lib/schema-components.js :

SchemaComponents = {
  school: {
    type: String,
    optional: false
  },
  // ...
  // more reusable components here
};

someCollectionFile.js :

Schemas.Student = new SimpleSchema({
    studentUserId: {
        type: String
    },
    school: SchemaComponents.school,
    // ...
});