2016-07-30 34 views
0

我可能会考虑错误的方式,请随时纠正我的想法。如何使用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** 
}); 

回答

1

您可以在可重复使用的对象移动到不同的文件,应该是在可见光客户端和服务器,如果你正在使用硅mpleSchema。

例如基于你的问题:

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, 
    // ... 
}); 
+0

谢谢,这几乎是正常工作。在我的例子中,我使用了'select'。出于某种原因,当我实施您的解决方案时,选择菜单不会出现。我错过了什么吗? – bp123

+0

对不起,我犯了一个愚蠢的错误。报废我的最后一条消息。您的解决方案完美运作谢谢 – bp123

+0

我只是试图在一个单独的模式中使用相同的组件,我不断收到错误,“ReferenceError:SchemaComponents未定义”我错过了什么? – bp123

相关问题