Meteor:根据数据上下文禁用按钮?

时间:2016-02-02 16:41:25

标签: meteor

我有一个多租户Meteor应用,并希望允许有权访问多个组织帐户的用户在组织之间切换。

切换组织的能力正在发挥作用,但我现在希望能够为用户的当前组织禁用button,因此他们无法切换到他们已经在的组织。

现在我尝试根据data-domain属性检索每个按钮的值,但它会返回undefined。此外,console.log(this);undefined的形式返回给帮助者。

如何获取数据上下文以将当前按钮的值与用户当前的组织相匹配?

这是模板:

<template name="switchAccountsModal">
  <div class="modal fade switchAccounts" id="switchAccounts" tabindex="-1" 
       role="dialog" aria-labelledby="switchAccounts">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal"
                  aria-label="Close">
             <span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title">Select an Organization</h4>
        </div>
        <div class="modal-body">
          <div class="list-group">
            {{#each organizations}}
            <!-- {{activeOrg}} should render 'disabled' if this equals the 
                 user's current organization. -->
            <button type="button" class="list-group-item switchAccount" 
                    data-domain="{{this}}" {{activeOrg}}>{{this}}</button>
            {{/each}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

这是帮手:

Template.switchAccountsModal.helpers({
  organizations: () => {
    var organizations = Meteor.user().organizations;
    console.log('organizations: ' + organizations);
    return organizations;
  },
  activeOrg: () => {
    console.log('activeOrg.this: ' + this);
    var currentOrg = Meteor.user().profile.currentOrg;
    var thisOrg    = $(this).data('domain');
    console.log('activeOrg.thisOrg: ' + thisOrg);
    return (this == currentOrg) ? {disabled: 'disabled'} : {};
  }
});

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

感谢Meteor Chef Slack频道中的天才(向 brajt stephendayta &amp; michelfloyd 喊出来),我能够通过并传递正确的数据上下文。虽然有几个选项,但对我有用的是在this空格键标记内传递参数{{activeOrg this}},然后在帮助程序中将其取出。

如果有其他人遇到此问题,请点击此处更新的代码:

<template name="switchAccountsModal">
  <div class="modal fade switchAccounts" id="switchAccounts" tabindex="-1"
       role="dialog" aria-labelledby="switchAccounts">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" 
              aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title">Select an Organization</h4>
        </div>
        <div class="modal-body">
          <div class="list-group">
            {{#each organizations}}
            <button type="button" class="list-group-item switchAccount"
                {{activeOrg this}}>
              {{this}}
            </button>
            {{/each}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

更新的助手:

Template.switchAccountsModal.helpers({
  organizations: () => {
    var organizations = Meteor.user().organizations;
    return organizations;
  },
  activeOrg: (org) => {
    let currentOrg = Meteor.user().profile.currentOrg,
        thisOrg    = org;
    return (currentOrg === thisOrg) ? {disabled: 'disabled'} : {};
  }
});

brajt 中的另一个选项是使用#each item in items而不是#each items。我之前尝试过,将#each变量中的实际按钮放入子模板中,但仍然无法传递数据上下文。

答案 1 :(得分:-2)

试试这个:

模板中的

<button disabled={{isDeactivated}}>Click me</button>

帮助者:

isDeactivated: function() {
  return true|false;
}