首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember.js计算属性的高级用法

问Ember.js计算属性的高级用法
EN

Stack Overflow用户
提问于 2013-05-15 23:59:08
回答 1查看 2.5K关注 0票数 1

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

以下是一些非常有用的简单用例:

代码语言:javascript
复制
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

我真的需要一些澄清-我怀疑我可能正在写一大堆令人敬畏的样板,我可以通过正确地使用这些样板来避免:smiley:。

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

EN

回答 1

Stack Overflow用户

发布于 2013-05-16 01:01:54

一些用例(虽然不是很高级,只是很琐碎)可能看起来像这样:

代码语言:javascript
复制
...
// 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是不可链接的(目前),所以这可能限制了它最高级的使用情况。但是如果它们是链式的,那么你可以这样做:

代码语言:javascript
复制
...
// not possible at the moment
showComments: Ember.computed.bool('content.post') && Ember.computed.bool('content.post.comments')
...

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

希望能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16569815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档