views:

88

answers:

1

So I had a requirement to add an undo function to my app and I discovered this nifty ChangeWatcher class, but it's not doing exactly what I want and I'm hoping someone knows how.

ChangeWatcher seems to monitor assignments to the variable reference. So when I say:

myXML = <xml/>

it fires off it's function just fine and dandy, but when I say:

myXML.appendChild(thisOtherXMLVar)

I get nothing, even though the variable changes the reference doesn't so ChangeWatcher doesn't fire off the function.

So does anyone know how to get ChangeWatcher to pick up on all changes to a variable?

+1  A: 

Hey,

ChangeWatcher is a really cool class. But it's not going to work unless the property you're trying to watch change is [Bindable].

If you're myXML variable is Bindable, then when you set it to the XML value, it will dispatch an event (default is propertyChange). ChangeWatcher adds an event listener to the object for whatever [Bindable] events have been declared. It does this by getting the Bindable metadata for that variable.

But the XML class itself doesn't have anything Bindable, so calling appendChild won't dispatch any events and ChangeWatcher wont work.

Updating based on your comment, you can just make all of your changes to the XML and then reset the variable. That will then cause the ChangeWatcher to register the update.

Hope that helps, Lance

viatropos
Well if by helps you mean it confirms my fears then it does :-/, Fortunately the workaround isn't too difficult. Instead of appending children to myXML directly, I create a new XML var, set it to new xml(myXML), append the children to the new var, then set myXML = new XML(the new xml var). In some ways this is preferrable since it will only fire the change handler once per function instead of several times per function.
invertedSpear
perfect, that will work well.
viatropos