【问题标题】:Is there a way to attach a template event based on the user role in Meteor?有没有办法根据 Meteor 中的用户角色附加模板事件?
【发布时间】:2014-03-02 05:39:08
【问题描述】:

我想知道,因为我还想不通,是否有办法附加 Meteor 的模板事件,例如:

Template.businessOperations.events({
    'dblclick .display': function (evt, tmpl) {
       //DO SOMETHING;
    }
}

基于当前用户的角色。

例如:

if (Meteor.user().profile.type === 'admin') {
  Template.businessOperations.events({
      'dblclick .display': function (evt, tmpl) {
         //DO SOMETHING;
      }
  }
}

我已经尝试过了,但没有成功,我也尝试从实际的事件方法中获取用户,但它也没有工作,我在这里没有看到正确的解决方案。

提前感谢您的帮助。

【问题讨论】:

  • 请记住,profile 字段默认情况下是用户可编辑的。 (它的意思是存储用户偏好的东西)所以把你的用户“类型”直接放在用户对象上。可以在此处找到执行此操作的相关软件包:github.com/alanning/meteor-roles
  • 会处理的,感谢您的建议。

标签: javascript node.js meteor handlebars.js


【解决方案1】:

仅当用户是管理员时才绑定事件很棘手,因为在评估模板代码时,用户可能没有登录。按照您的建议,在事件处理程序内部进行检查似乎很完美合理的。我会这样写:

Template.businessOperations.events({
  'dblclick .display': function() {
    if (Meteor.user() && (Meteor.user().profile.type === 'admin')) {
      console.log('double click and admin!');
    }
  }
});

我添加了对Meteor.user() 的额外检查,以防止出现用户未登录的情况。如果不可能,您可以将其删除。

【讨论】:

  • 我要求 Meteor.user().profile 代替,它可以工作。
猜你喜欢
  • 2020-07-12
  • 2021-10-12
  • 2020-06-10
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 2019-10-10
  • 2018-03-27
  • 2011-06-22
相关资源
最近更新 更多