Emberjs - 临时禁用属性更改通知 [英] Emberjs - Temporary disable property changes notification

查看:215
本文介绍了Emberjs - 临时禁用属性更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道您可以使用推迟他们的任何简单的方法来暂时禁用对象属性或属性的通知beginPropertyChanges() endPropertyChanges()但是我不希望这些更改在我明确启用它们之前被通知。



提前谢谢。



用例:
我必须设置另一个对象( B )的对象属性( A )。通过其他对象的几种方法观察 B 的属性。在某些时间, B 对象的数据被清除,观察者得到通知,稍后HTTP响应将它们设置为有用的东西。我不希望观察者在清除对象时收到通知,因为该属性值在此时无效。

解决方案

一个旧的问题,但Google搜索中似乎很高,暂停观察员,所以我会发表评论。



有一个明显的用例,例如一个枚举对象的属性由列表框表示,对象可能会更改。如果列表框用于从服务器请求属性更改并在成功时设置新值,那么自然的做法就是使用控件属性与列表框,当对象更改时将该属性设置为对象属性,并观察它向服务器发出请求。在这种情况下,每当对象发生变化时,观察者将收到一个不需要的通知。



但是,我同意这些用例是如此多样化,Ember可以支持他们



因此,可能的工作是在控制器中声明一个变量,并在更改属性时使用它,以便只对用户所做的更改做出反应:

  doNotReact:false,
update手动:function(){
this.doNotReact = true;
Ember.run.scheduleOnce('actions',this,function(){
this.doNotReact = false;
});
............
this.set('something',somevalue);
............
},

onChange:function(){
if(this.doNotReact)return;
...............
} .observes('something')

在观察者有机会运行后,doNotReact的值将被重置。我不建议在观察者中重置塞子变量,因为如果您将属性设置为相同的值,它将不会运行。


Is there any simple way to achieve a temporary disabling of notifications of an object property or properties?

I know you can defer them with beginPropertyChanges() and endPropertyChanges() but I don't want those changes to be notified at all until I explicitly enable them.

Thank you in advance.

Use case: I have to set a property of an object (A) with another object (B). Properties of B are being observed by several methods of other objects. At some time the B object's data gets cleared and the observers get notified, later an HTTP response sets them with something useful. I would not want the observers get notified when clearing the object because the properties values are not valid at that moment.

解决方案

This is an old question, but it appears high in Google search for suspending observers, so I would comment.

There are evident use cases for such a feature, for example, an enum property of an object is represented by a list box and the object may change. If the list box is used to request property changes from the server and to set the new value on success, the natural way to do things is to use a controller property with the list box, set that property to the object property when the object changes, and observe it to make requests to the server. In this case whenever the object changes the observer will receive an unwanted notification.

However, I agree that these use cases are so diverse that there is no way Ember can support them.

Thus, a possible work around is to declare a variable in the controller and use it whenever you change a property so that you react only to changes made by the User:

doNotReact: false,
updateManually: function() {
    this.doNotReact = true; 
    Ember.run.scheduleOnce('actions', this, function() {
       this.doNotReact = false;
    });
    ............
    this.set('something', somevalue);
    ............
},

onChange: function() {
    if (this.doNotReact) return;
    ...............
}.observes('something')

The value of doNotReact will be reset after the observer gets a chance to run. I do not recommend to reset the stopper variable in the observer since it will not run if you set the property to the same value it already has.

这篇关于Emberjs - 临时禁用属性更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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