【问题标题】:Ember.js advanced uses of Computed PropertiesEmber.js 计算属性的高级使用
【发布时间】:2013-05-15 15:59:08
【问题描述】:

我一直在查看https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/computed.js#L482,发现计算属性实际上比指南中记录的要强大得多。

一些真正有用的直接用例是:

Ember.Object.create({
  propertyA: Ember.computed.empty('anotherProperty'),
  propertyB: Ember.computed.not('anotherProperty'),
  propertyB: Ember.computed.equal('anotherProperty', "Ed Balls")
});

但我真的不明白更高级的案例是如何工作的:https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/computed.js#L617

我真的可以澄清一下——我怀疑我可能正在编写大量的样板文件,如果正确使用这些样板文件,我可以避免这些样板文件:笑脸:。

一旦我了解了它们的工作原理,我绝对可以尝试在指南中添加一些文档。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    一些用例(虽然不是那么高级,只是微不足道)可能看起来像:

    ...
    // this could be in your controller
    hasPost: Ember.computed.bool('content.post')
    hasComments: Ember.computed.bool('content.post.comments')
    showComments: Ember.computed.and('hasPost', 'hasComments')
    // here you could perfectly bind on the showComments property
    ...
    

    您应该认真考虑的一件事是 CP 不能链接(目前),所以这可能限制了它的最高级使用情况。但是,如果他们可以链接,那么您可以执行以下操作:

    ...
    // not possible at the moment
    showComments: Ember.computed.bool('content.post') && Ember.computed.bool('content.post.comments')
    ...
    

    可以在here 找到一些用例(尽管只有测试用例)。 这个issue 可能也很有趣。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2013-06-29
      • 2017-12-18
      • 2013-11-29
      • 2012-07-27
      • 2020-12-10
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2016-08-13
      相关资源
      最近更新 更多