通过带把手的寄存器助手渲染对象

时间:2013-10-14 14:24:56

标签: html templates handlebars.js helper

我的目标是这样的

  {user:{name:"user1", role:"admin", roleId:1, status:1 },
  users:[{id:1, name:"user1", role:"admin", roleId:1, status:1 }, {id:2, name:"user2",   role:"user", roleId:2, status:2 }]
  roles:[{roleId:1, name="user"},{roleId:2, name="admin"}]
  };

我的模板是这样的。我正在迭代用户

  {{#users}}
 <div id="userRoleInfo">
            <input type="hidden" id="userId" value="{{id}}" />
            <div class="label">
                <label>User permission</label>
            </div>
            {{#checkUser ../user.roleId roleId ../roles}}
                <div >Administrator</div>
            {{else}} 
                <select id="selRoleId" data-custom="true">                                                   
                        <option value="0" >Assign Role</option>

                    //Problem is here. Its not displaying roles
                    {{#each roles}}
                        <option value="{{roleId}}">{{name}}</option>
                    {{/each}}

                </select> 
            {{/checkUser}}

       </div>

{{/users}} 

我的注册助手是这样的

    Handlebars.registerHelper("checkUser", function(loggedInUserRoleId, roleId,roles,  options) {
    if(loggedInUserRoleId == 1 && roleId < 2){
   return options.fn(this);
    }else{
  //return options.inverse(roles);
  return options.inverse(this); // I am not sure what should I do here so that it could display the roles in select tag
         }

    });

所以我不知道如何使用roles对象在select标签内显示角色。

如果有混淆或需要更多信息,请告诉我

1 个答案:

答案 0 :(得分:1)

{{else}}内部的行为与在任何其他区块中的行为相同,因此您需要../足够的时间来回到roles生活的地方:

{{#each ../../roles}}
    <option value="{{roleId}}">{{name}}</option>
{{/each}}