有没有办法监控对象的更改? [英] Is there a way to monitor changes to an object?

查看:107
本文介绍了有没有办法监控对象的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相反,根据用户操作触发事件,您可以只监听对象的更改并做出反应吗?

Instead triggering an event based on user action can you just listen for changes to an object and react then?

推荐答案

,根据您使用的域名,您可以使用 backbone.js - 它有一个模型您可以扩展并连接到在模型更改时自动由事件触发的回调的对象 - 也就是说,您在模型上设置了一个字段,它将自动触发您可以侦听的事件。您必须定义模型以扩展其模型,并调用setter函数而不是直接设置属性,但它非常有用,我推荐它。

Well, depending on the domain you're working with, you can use backbone.js - it has a "model" object that you can extend and connect up to callbacks that get triggered by events automatically when the model is changed - that is, you set a field on the model, and it will automatically fire an event that you can listen to. You have to define your models to extend its Model, and call a setter function instead of setting properties directly, but it's very useful, I recommend it.

为了清楚起见,事件在更改的模型中被触发,因此即使您以编程方式或在控制台中设置属性的值,它也会被触发。

Just for clarity's sake, the event is fired from the model on change, so it's fired even if you set the value of the attribute programmatically or in the console.

这篇关于有没有办法监控对象的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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