如何在模型对象上观察* all *属性更改? [英] How do I observe *all* property changes on a model object?

查看:111
本文介绍了如何在模型对象上观察* all *属性更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从JSON对象构建的模型。

I have a model built from a JSON object.

// extend the json model to get all props
App.Model = Ember.Object.extend(window.jsonModel);

我想在任何更新时自动保存模型。有没有什么办法可以在整个模型中添加一个观察者?

I want to automatically save the model when anything is updated. Is there any way I can add an observer to the whole model?

编辑://添加我目前去的解决方案

// adding the solution I currently go

现在我做:

// XXX Can't be right
for (var prop in window.jsonModel) {
    if (window.jsonModel.hasOwnProperty(prop)) {
        App.model.addObserver(prop, scheduleSave);
    }
}

这是一个很大的表单,这意味着我增加了大量观察员 - 似乎效率低下。

This is a large form, which means I'm adding tons of observers – it seems so inefficient.

Ember.sendEvent()中的firebug断点显示有一些名为 App .model.lastName:更改正在发送。我可以在那里进行拦截,但是希望能够正式运行。

A firebug breakpoint at Ember.sendEvent() reveals that there are events called App.model.lastName:change being sent. I could hack in an intercept there, but was hoping for an official way.

推荐答案

你可以绑定到 isDirty 属性 DS.Model 的子类。当模型属性之一更改时, isDirty false 更改为 true 。它不适用于所有情况,因为它仅在重置或提交之前更改一次,但对于您的情况 -

You can bind to isDirty property of subclass of DS.Model. The isDirty changes from false to true when one of model properties changes. It will not serve well for all cases because it changes only once until reset or committed, but for your case -


我想自动保存当任何东西被更新时的模型。有没有什么办法可以在整个模型中添加观察者?

I want to automatically save the model when anything is updated. Is there any way I can add an observer to the whole model?

它可以正常工作。

这篇关于如何在模型对象上观察* all *属性更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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