#if与Roles.userIsInRole无法正常工作

时间:2015-06-16 14:47:33

标签: meteor meteor-blaze meteor-accounts meteor-helper

我正在创建一个模式来管理用户的权限。但是当加载模态时。如果用户具有该角色,则控制台将从服务器返回true。虽然{{#if hasRole}}仍然显示错误的标签。

editUserPermissionsModal.html

<tbody>
    {{#each roles}}
    <tr>
        <td>{{this.name}}</td>
        <td>
            {{#if hasRole}}
            <span class="label label-success">Toegang</span> 
            {{else}}
            <span class="label label-danger">Geweigerd</span> 
            {{/if}}
        </td>
        <td>
            <select class="roleSelect">
                {{#if hasRole}}
                <option value="allow">Toestaan</option>
                <option value="deny">Weigeren</option>
                {{else}}
                <option value="deny">Weigeren</option>
                <option value="allow">Toestaan</option>
                {{/if}}
            </select>
        </td>
    </tr>
    {{/each}}
</tbody>

editUserPermissionsModal.js

Template.editUserPermissionsModal.helpers({
  roles: function() {
    return Roles.getAllRoles();
  },

  hasRole: function(){
    var userId = Session.get("editing_user");
    var role = this.name;
    Meteor.call("checkRole", userId, role, function(error, result){
      if(error){
        console.log("error", error);
      }
      if(result){
        console.log(result);
        return result;


      }
    });
  }

});

Template.editUserPermissionsModal.events({
  "change .roleSelect": function(event, template){

    var addRole = event.target.value;

    if(addRole == 'allow') {
      var user = Session.get("editing_user");
      var role = this.name;

      Meteor.call('addRoleToUser', user, role)

    }

  }
});

server.js

checkRole:function(userId, role) {
    return Roles.userIsInRole(userId, role);
  }

1 个答案:

答案 0 :(得分:0)

这是典型的“助手中的方法调用”问题。你可以阅读它here

鉴于您需要为您的方法设置变量参数,您的案例中最简单的解决方案可能是使用David Weldon's answer并安装Sashko的meteor-reactive-method包:

$ meteor add simple:reactive-method

然后:

Template.editUserPermissionsModal.helpers({
  roles: function() {
    return Roles.getAllRoles();
  },

  hasRole: function(){
    var userId = Session.get("editing_user");
    var role = this.name;
    var result = ReactiveMethod.call("checkRole", userId, role);
    console.log(result);
    return result;
  }
});