EmberJS 计算 `and` 的否定

EmberJS computed `and` negation

我有一个 class 属性:

import { and, not } from '@ember/object/computed';

EmberObject.extend({
    a: false,
    notA: not('a'),

    b: true,
    c: true,
    d: and('b', 'c', 'notA')
});

所以d依赖于a的否定。

是否有像 d: and('b', 'c', '!a') 这样的符号,这样我就不需要多余的 notA 属性?

Is there a notation to do something like...

没有。但是,您可以对完整的计算进行编程:

d: computed('a', 'b', 'c', function() {
  return !this.a && this.b && this.c;
}),

但是你可以用这样的东西自己构建它:

function betterAnd(...props) {
  const positiveDeps = props
    filter(x => !x.startsWith('!'));
  const negativeDeps = props
    .filter(x => x.startsWith('!'))
    .map(x => x.substr(1));
  return computed(...positiveDeps, ...negativeDeps, function() {
    return [
      ...positiveDeps.map(x => this.get(x)),
      ...negativeDeps.map(x => !this.get(x)),
    ].reduce((a, b) => a && b, true);
  });
}

然后你就可以 d: betterAnd('b', 'c', 'notA').

你可以使用ember-awesome-macros,它支持组合宏:

import { and, not } from 'ember-awesome-macros';

EmberObject.extend({
    a: false,
    b: true,
    c: true,
    d: and('b', 'c', not('a'))
});