2016-07-12 3 views
0

Итак, у меня есть коллекция семантического типа для определения моего типа. Он имеет поле arrayValue. Функциональность - это новый семантический тип, который может быть массивом существующих семантических типов, предопределенных или определенных пользователем. Проблема в том, что все семантические типы хранятся в самой коллекции. Так можно ли получить значения в коллекции (что-то вроде SemanticTypes.findOne(). SemanticType) при определении схемы? Вот код:
коллекция:Получите все текущие значения в коллекции при определении своих символов chema

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> 

ЯШ:

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

Я не верю, что вы хотите поместить все различные значения 'semanticType' в массив, который сам в схемы. Вы пытаетесь создать меню в autoform? –

+0

да. Моя логика заключается в том, что существует возможность создать массив существующих семантических типов. Поэтому, когда вы выбираете «массив», появляется всплывающее меню с опцией autoform, позволяющее мне выбирать из существующих семантических типов – yiyizheliu

ответ

0

Я на самом деле нашел способ сделать это путем определения переменных параметров в помощнике для получить все значения в коллекции с помощью collection.find() и использовать {{> afQuickField name="arrayType" options=arrayOptions}} в html. Вот мой код:

ЯШ:

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>