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

查看:24
本文介绍了如何观察模型对象上的 *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() 处的萤火虫断点显示有名为 App.model.lastName:change 的事件正在发送.我可以在那里进行拦截,但希望有官方途径.

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.

推荐答案

您可以绑定到 DS.Model 子类的 isDirty 属性.当模型属性之一发生变化时,isDirtyfalse 变为 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天全站免登陆