流星afFormGroup没有填充下拉框

时间:2015-06-02 12:20:23

标签: javascript meteor

html如下:

<template name="addUser">
  {{#autoForm id="addUser" schema=schema meteormethod="newUser"}}
  <fieldset>
        {{> afQuickField name="fName"}}
        {{> afQuickField name="lName"}}
        {{> afQuickField name="username"}}
        {{> afQuickField name="email"}}
        {{> afFormGroup name="roles" firstoption="Select Role"    options=addUseroptions type="select"}}
        {{> afQuickField name="password"}}
    <div>
      <button type="submit" class="btn btn-primary">Submit</button>
      <button type="reset" class="btn btn-default">Reset</button>
    </div>
  </fieldset>
  {{/autoForm}}
</template>

afFormGroup角色未填充。我正在尝试使用当前存储在mongodb中的所有角色填充下拉框,但下拉列表仍为空。控制台中的console.log()语句显示正确的值。

js如下:

Template.addUser.helpers({
    schema : function (){
        return MySchema.addUser;
    },
    collection : function (){
        return MyCollections.Users;
    },
    addUseroptions: function (){
      var array = [];
      var array2 = [];
      roles = Roles.getAllRoles();
      array = roles.fetch(); 

      for(var i = 0; i < roles.count(); i++){
      array2[i] = array[i].name;
      }
      console.log(roles.count());
      console.log(array2);

      return
      [{
        optgroup: "Roles",
        options: array2
      }];
    }
  });

1 个答案:

答案 0 :(得分:1)

你的帮助功能似乎有点困惑。试试这个:

// Still inside your "helper" block
addUseroptions: function() {
  return Roles.getAllRoles().map(function(r) {
    // "r" here is a single document returned by the cursor. Use this
    // space to do whatever change you wanna do in respect to
    // label name or select value
    return {label: r.name, value: r.name};
  });
}