Ember.js 2基于同一依赖项的计算属性仅触发一次
基于同一依赖项有两个计算属性,则只运行一个计算属性。文档中说它是缓存的,那么当我想要以下内容时会发生什么情况呢:Ember.js 2基于同一依赖项的计算属性仅触发一次,ember.js,Ember.js,基于同一依赖项有两个计算属性,则只运行一个计算属性。文档中说它是缓存的,那么当我想要以下内容时会发生什么情况呢: foo: (-> console.log 'foo' ).property('dependency') bar: (-> console.log 'bar' ).property('dependency') 现在没有调用条,我不得不求助于observer。我能让它工作吗 编辑 这个问题是关于计算属性的,但它没有反映在示例代码中,而不是
foo: (->
console.log 'foo'
).property('dependency')
bar: (->
console.log 'bar'
).property('dependency')
现在没有调用条
,我不得不求助于observer。我能让它工作吗
编辑 这个问题是关于计算属性的,但它没有反映在示例代码中,而不是我使用的
属性。现在改变了。抱歉搞混了
编辑#2
我用@MilkyWayJoe修改了这个伟大的例子,使它现在看起来像我的解决方案。不幸的是(或幸运的是)它起作用了,但我的解决方案没有起作用。以下是我所追求的要点:
在滑块的帮助下,我可以设置一个余额值以转移到另一个信用卡提供商。假设该值为transferValue
。每当它发生变化时,我都要计算加入转让费的年利息
比如说,我现在的信用卡上有1000美元,年利率是19%。这太多了,所以我寻找另一个更便宜的解决方案。原来X银行提供余额转账信用卡Y,利率为10%+3.5%的转账费
嗯。所以我在滑块上设置了1000美元,这就是魔术。我想计算利率和转让费时,价值的变化
在修改后的示例中,它可以工作:
但在我的第一个解决方案中没有做到这一点。这是一种:
cardInterest: (->
apr = @get 'purchaseRate'
amount = @get 'transferValue'
@get('calculatedTransferFee') + @calculateInterest apr, amount
).property('transferValue', 'calculatedTransferFee')
正如您所看到的,它访问calculatedTransferFee
。问题是没有重新计算该值。我不确定这是否值得一提,但在第一个解决方案中,Handlebar模板只请求了cardInterest
这是我目前使用observer的解决方案:
calculatedTransferFee: 0
transferValueDidChange: (->
if @get('isCurrentCardChosen')
transferFee = parseFloat @get('balanceTransferRate') / 100
transferValue = @get 'transferValue'
calculatedTransferFee = if isNaN(transferFee) then 0 else transferFee * transferValue
@set 'calculatedTransferFee', calculatedTransferFee
).observes('transferValue')
这不是一个好的解决方案,是吗?这就是为什么我认为这可能是一个比求助于观察者更好的解决方案
我希望现在情况更清楚了。如果有任何进一步的反馈,我将不胜感激 不太确定我是否理解您试图实现的目标,因为您正在谈论属性
,但您的代码正在使用观察
(我知道这是一个概念示例代码,只是不确定您要去哪里)
通常,property
将是一个值的“反应式访问器”,99.9%的时间您希望缓存它(默认情况下,除非您说.property('whatever')。volatile()
),而observe
将在它监视的任何属性更改时启动函数。如果只想为同一依赖项触发两个属性,可以:
App.SomeModel = Em.Object.extend({
someDependency: true,
foo: function() {
// all that's in here will fire only once when 'dependency' changes, and store
// the returning value in cache, and every time something reads this property,
// it will retrieve the cached value.
// A way to test this, is to run the following from your View:
// "alert(this.controller.get('content.foo'));"
// It will alert the string but will not log "whatever bro" again.
console.log('whatever bro');
return "this.foo %@ a dependency".fmt(this.get('someDependency') ? "has" : "doesn't have");
}.property('someDependency'),
bar: function() {
// same as above
console.log('whatever dude');
return "this.bar %@ a dependency".fmt(this.get('someDependency') ? "has" : "doesn't have");
}.property('someDependency'),
nope: function() {
// same as above, except this is volatile
// and will fire the console.log every time
console.log('y\'all need science');
return "this.nope %@ a dependency".fmt(this.get('someDependency') ? "has" : "doesn't have");
}.property('someDependency').volatile()
});
(见附件)
如果每次都需要记录它(可能是为了调试或其他原因),可以使用volatile
或observes
如果我被绊倒了,而这不是您想要的,也许您可以重新表述您的问题或刷新示例代码,使其更接近您的真实场景,以澄清所问的问题。您能提供一个(非)工作示例吗?您确定正在请求您的两个计算属性(使用get on js或on template)?“如果不叫它,它将永远不会被计算出来。”雷南托·马尔弗南德斯,这很有趣。只有第一个,foo
在模板中被请求-另一个,bar
不是,但我在foo
中需要它-它是通过@get'foo'请求的。但正如我所说的,由于缓存的依赖关系
,它没有被触发。感谢您提供的详细示例!我试图修改它,使其与实际解决方案相似,但没有成功。请查看更新的问题以了解更多信息。@wojtekrych我以前没有看到此评论。我今天晚些时候再查