2013-12-11 39 views
0

我在Meteor.js上使用了一個帶autoform的車手助手。我試圖在我的表單上做一個選擇下拉框,但我希望選項來自集合而不是數組。我已經使用collection2包定義了我的集合「Persons」,並使用簡單模式定義了模式。我已經插入了名字,姓氏,全名值的兩個人。如何在車把手助手中使用集合

這裏是我的助手:

Handlebars.registerHelper("personSelectOption", function(options) { 
peeps = Persons.find().fetch(); 

peeps.forEach(function(persons){ 

    return [ 
    {label:persons.firstName , value:persons.firstName} 
]; 
}); 
}); 

我試圖讓下拉框中顯示的每個人,我在我的收藏,並作爲中的firstName我加上更多的人來收集它會在自動顯示下拉框。

我知道我在這裏錯過了很多,但我是一個新的編碼器,任何幫助我都會很棒。

謝謝!

我想使用手柄幫手,因爲我看到使用autoform的唯一好例子也是使用這個幫手。自動方式下拉框使用此:

<div class="form-group {{afHasError 'firstOptionSelect'}}"> 
     {{afFieldLabel 'firstOptionSelect'}} 
     {{afFieldInput 'firstOptionSelect' firstOption="(Select Something)" options=personSelectOption}} 
     {{#if afFieldIsInvalid 'firstOptionSelect'}} 
     <span class="help-block">{{afFieldMessage 'firstOptionSelect'}}</span> 
     {{/if}} 
     </div> 

選項是我試圖讓firstName出現。而forEach則是爲了獲得收藏中的每一個名字。我如何使用流星語法來使用自動形式而不使用把手助手?

感謝

+0

你爲什麼使用'Handlebars.registerHelper'?你讀過文檔嗎?只需通過在模板上定義一個函數來添加一個幫助器:'Template.myTemplate.myHelper = function(){};' – sbking

回答

0

這是結束了,我的工作:

車把幫手:

Handlebars.registerHelper("personSelect", function() { 

peeps = Persons.find(); 


people = peeps.map(function(peeps) { 

return {label:peeps.fullName, value:peeps.fullName }; 

}); 
return people; 
}); 

而對於自動窗體模板:

<div class="form-group {{afHasError 'firstOptionSelect'}}"> 
     {{afFieldLabel 'firstOptionSelect'}} 
     {{afFieldInput 'firstOptionSelect' firstOption="(Select Something)" options= personSelect }} 
     {{#if afFieldIsInvalid 'firstOptionSelect'}} 
     <span class="help-block">{{afFieldMessage 'firstOptionSelect'}}</span> 
     {{/if}} 
     </div> 

我不使用流星Template.helper因爲我需要在autoform模板中顯示模板。我還沒有找到辦法做到這一點,所以我決定改用handlebars.helper。如果有人知道如何做到這一點,我是全部耳朵。

0

由於Cuberto說,流星給你的助手添加到使用流星自己的語法模板的能力。它也不清楚你試圖用forEach塊做什麼,但這樣的事情應該工作:

Template.myTemplate.helpers({ 
    personSelectOption: function() { 
    return Persons.find().fetch(); 
    } 
}); 

然後你可以像下面這樣獲得想要的信息在html

<template name="myTemplate"> 
    <select> 
    {{#each personSelectOption}} 
     <option value="{{this.firstName}}">{{this.firstName}}</option> 
    {{/each}} 
    </select> 
</template> 

您可以在模板中訪問您需要的任何文檔屬性,而無需構建一些特殊結構並從幫助程序返回。雖然如果你真的想這樣做(因爲你需要以某種方式改變屬性),你應該使用map而不是forEach來返回修改對象的單個數組。