观察者如何找到Ember.js中所观察到的属性的前后值? [英] How can an observer find out the before and after values of the observed property in Ember.js?

查看:89
本文介绍了观察者如何找到Ember.js中所观察到的属性的前后值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅ud3323提供的另一个问题的解决方案: http://jsfiddle.net/ud3323/ykL69/ 。此解决方案使用红色突出显示更改的值。我有一个额外的要求:如果该值增加了绿色的高亮,如果它的红色突出显示减少。为此,我需要知道观察者中的新旧价值。有没有办法这样做?

Please see the solution to another question provided by ud3323: http://jsfiddle.net/ud3323/ykL69/. This solution highlights the changed value using the red color. I have an additional requirement: if the value has increased highlight in green color, if it has decreased highlight in red color. For this, I need to know the old and the new values in my observer. Is there any way to do this?

Embers文档不会对观察器功能有什么用。从示例中我可以看出,由于观察者在itemRowView中定义,this指向itemRowView。

P.S. Embers docs do not say anything about what's available in the observer function. All I can tell from the example is that since the observer is defined in the itemRowView, "this" points to the itemRowView.

推荐答案

看看 beforeObserver s。一个 beforeObserver 在属性更改之前触发。因此,如果您在beforeObserver中获取属性,那么您将具有预更改值。您使用 Ember.addBeforeObserver 或通过 observesBefore 函数原型扩展名设置此类型的观察者。

Take a look at beforeObservers. A beforeObserver fires before a property changes. So if you get the property in a beforeObserver, you will have the the pre-change value. You set up this type of observer using Ember.addBeforeObserver or via the observesBefore function prototype extension.

这将给你你需要达到你的目标。我根据你的小提琴创建了以下JSBin,以便在这里展示: http:// emberjs。 jsbin.com/qewudu/2/edit

That would give you what you need to achieve your goal. I've created the following JSBin based on your fiddle to demonstrate this here: http://emberjs.jsbin.com/qewudu/2/edit

更新于2014-10-07以反映Ember 1.7.x中的行为。

UPDATED on 2014-10-07 to reflect behavior in Ember 1.7.x.

2015-02上的更新: beforeObserver 已被弃用。查看此问题的其他答案。

UPDATE on 2015-02: beforeObserver has been deprecated. See other answers to this question.

这篇关于观察者如何找到Ember.js中所观察到的属性的前后值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆