Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to monitor changes to an object?

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

like image 384
fancy Avatar asked Jun 02 '11 15:06

fancy


People also ask

What is watch in JavaScript?

The watch() method watches for the event in which a property gets assigned a value.


1 Answers

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.

like image 106
Matt Avatar answered Oct 26 '22 11:10

Matt