在定义简单chema时,获取集合中的所有当前值

时间:2016-07-12 23:23:26

标签: meteor meteor-autoform

所以我在这里有一个语义类型集合来定义我的类型。它有一个字段arrayValue。功能是新的语义类型,这里可以是现有语义类型的数组,可以是预定义的,也可以是用户定义的。问题是所有语义类型都存储在集合本身中。那么在定义模式时是否可以获取集合中的值(类似于SemanticTypes.findOne()。semanticType)?这是代码:
系列:

SemanticTypes = new Meteor.Collection('semanticTypes');
Schemas.SemanticTypes = new SimpleSchema({
   semanticType: {
      type: String,
      regEx: /^[a-zA-Z_][a-zA-Z0-9_]*$/,
      unique: true
    },
    baseType:{
      type: String,
      autoform: {
        options: [
          {label: "int", value: "int"},
          {label: "float", value: "float"},
          {label: "string", value: "string"},
          {label: "bool", value: "bool"},
          {label: "array", value: "array"}
         ]
        }
       },
       arrayValue: {
          type: String,
          optional: true,
          autoform: {
             options: 
              // I want to get all current semantic types in the collection now
          }
        }
});
SemanticTypes.attachSchema(Schemas.SemanticTypes);

HTML:

<template name="addSemanticTypeForm">
 {{#autoForm collection="SemanticTypes" id="insertSemanticTypeForm" type="insert"}}
     <fieldset>
         {{> afQuickField name='semanticType'}}
         {{> afQuickField name='baseType' }}
         {{#if isArraySelected}}
             {{> afQuickField name='arrayValue'}}
         {{/if}}
     </fieldset>
     <button type="submit" class="btn btn-primary">Add</button>
 {{/autoForm}}
 </template>

JS:

Template.addSemanticTypeForm.onCreated(function() {
    Session.set("isArraySelected", false);
});

Template.addSemanticTypeForm.helpers({
  isArraySelected: function() {
    return Session.get("isArraySelected");
   }
});

Template.addSemanticTypeForm.events({
   'change select[name=baseType]': function(evt) {
      if ($(evt.currentTarget).val() == "array"){
          Session.set("isArraySelected", true);
       }
     else {
         Session.set("isArraySelected", false);
       }
    }
   });

1 个答案:

答案 0 :(得分:0)

我实际上找到了一种方法,通过在帮助器中定义选项变量来获取集合中collection.find()的所有值,并在html中使用{{> afQuickField name="arrayType" options=arrayOptions}}。这是我的代码:

JS:

Template.addSemanticTypeForm.onCreated(function() {
Session.set("isArraySelected", false);
});

Template.addSemanticTypeForm.helpers({
   isArraySelected: function() {
   return Session.get("isArraySelected");
   },
   arrayOptions: function(){
      var options = [];
      SemanticTypes.find().forEach(function(type){
         options.push({label: type.semanticType, value: type.semanticType});
      });  
      return options;
     }
});

 Template.addSemanticTypeForm.events({
   'change select[name=baseType]': function(evt) {
    if ($(evt.currentTarget).val() == "array"){
       Session.set("isArraySelected", true);
     }
   else {
     Session.set("isArraySelected", false);
   }
  }
 });

HTML:

<template name="addSemanticTypeForm">
  {{#autoForm collection="SemanticTypes" id="insertSemanticTypeForm" type="insert"}}
   <fieldset>
       {{> afQuickField name='semanticType'}}
       {{> afQuickField name='baseType' }}
       {{#if isArraySelected}}
           {{> afQuickField name='arrayValue' options=arrayOptions}}
       {{/if}}
  </fieldset>
   <button type="submit" class="btn btn-primary">Add</button>
{{/autoForm}}
</template>
相关问题