簡體   English   中英

Ember.js常用計算屬性模式的簡寫

[英]Ember.js shorthand for common computed property pattern

在Ember.js中,我發現自己定義的計算屬性如下所示:

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.prop")

您也可以使用alias來設置此屬性。 給定一個實現上面給出的屬性的Ember對象,您可以:

obj.set("someProp", "foo or whatever"); // The set will be propagated to otherObject.prop

鏈接到Ember.computed.alias的Ember Source


更新:Ember.computed.oneWay

最近,一個新的計算屬性速記( oneWay )被添加到Ember,這對於這個要求也是可行的。 不同之處在於oneWay簡寫只適用於get case 因此,在創建對象時,這種速記比更復雜的alias更快。

someProp: Ember.computed.oneWay("otherObject.prop")

鏈接到Ember.computed.oneWay的Ember Source

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM