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

查看:16
本文介绍了观察者如何在 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.

推荐答案

看看 beforeObservers.beforeObserver 在属性更改之前触发.因此,如果您在 beforeObserver 中 get 属性,您将拥有更改前的值.您可以使用 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天全站免登陆