Ember.js通用计算属性模式的简写 [英] Ember.js shorthand for common computed property pattern
问题描述
someProp:function(){
return this.get('otherProp');
} .property('otherProp')
或
someProp:function(){
return this.get('otherObject.prop');
} .property('otherObject.prop')
是否有较短的写入方式计算的属性遵循这些模式?
经过研究,你可以干一点,帮助 Ember.computed.alias :
someProp:Ember.computed.alias(otherObject。支持)
您可以使用别名
设置此属性。给出一个实现上面给出的属性的Ember对象,你可以执行以下操作:
obj.set(someProp,foo或随你); //该集合将被传播到otherObject.prop
链接到Ember.com的Ember Source for Ember.computed.alias
更新:Ember.computed.oneWay
最近,一个新的计算属性简写( oneWay
)被添加到Ember,这也是可行的。不同的是, oneWay
简写仅适用于获取案例。因此,在对象创建过程中,这种速记比更复杂的别名
更快。
someProp:Ember.computed.oneWay(otherObject.prop)
/ pre>
链接到Ember.com的Ember源for Ember.computed.oneWay
In Ember.js I find myself defining computed properties that look like this:
someProp: function(){ return this.get('otherProp'); }.property('otherProp')
or
someProp: function(){ return this.get('otherObject.prop'); }.property('otherObject.prop')
Is there a shorter way to write computed properties that follow these patterns?
解决方案Having researched a little bit you could dry this a little up by doing the following with the help of Ember.computed.alias:
someProp: Ember.computed.alias("otherObject.prop")
You can use
alias
also to set this property. Given an Ember object which implements the property given above, you can do:obj.set("someProp", "foo or whatever"); // The set will be propagated to otherObject.prop
Link to Ember Source for Ember.computed.alias
Update: Ember.computed.oneWay
Recently a new computed property shorthand (
oneWay
) was added to Ember, which is also feasible for this requirement. The difference is that theoneWay
shorthand only works in the get case. Therefore this shorthand is faster during object creation than the more complexalias
.someProp: Ember.computed.oneWay("otherObject.prop")
Link to Ember Source for Ember.computed.oneWay
这篇关于Ember.js通用计算属性模式的简写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!