我一直在研究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
我真的需要一些澄清-我怀疑我可能正在写一大堆令人敬畏的样板,我可以通过正确地使用这些样板来避免:smiley:。
一旦我了解了它们的工作原理,我肯定可以尝试在指南中添加一些文档。
发布于 2013-05-16 01:01:54
一些用例(虽然不是很高级,只是很琐碎)可能看起来像这样:
...
// 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可能也很有趣。
希望能有所帮助
https://stackoverflow.com/questions/16569815
复制相似问题