views:

46

answers:

2

Hello,

I'm creating an air application that allows the user to create, edit and save Objects in files. I'm trying to implement a "Save on Close" prompt if the user hasn't saved her changes to the object before closing.

Let's say I have a Class called MyClass marked as Bindable that is composed of fields, each exposing a getter and setter. Some of these fields are ArrayCollections of custom Classes. I want to consider a change a change to any of the fields, or any of the fields in any of the objects in any of the array collections the parent object is made up of.

I could manually dispatch an event anytime a setter is called on the MyClass object and manage this all manually, but I'd also have to do it in a bunch of places in the application logic, for instance when the user changes one of the custom objects in one of the array collections that comprises it. This is a fine solution, but a little dirty and hard to maintain. I could also check the current object against a copy I made at the time of start up or last save, but I don't like the idea of doubling my memory footprint just for this.

I was hoping there was some event, perhaps an event that the binding broadcasts, that I could listen for in one place and manage my "Saved" variable. Does such a thing exist? I tried listening for the dataChange event that binding apparently dispatches, but I either did it wrong, or it doesn't do what I think it should. Is there a better option?

Thanks

+1  A: 

Hi, I don't know if this could be related, but you can always have a look

just_a_dude
+1  A: 

Create a hash for your object that get saved and then compare the saved hash to the hash of the object that's currently being manipulated. If it differs, your objects are different.

This of course requires a decent hash with a sufficiently low amount of collisions, but there are numerous hash functions available online at your perusal.

There is the "propertyChanged" event that you can use for bindable properties, but it's brittle at best since it only applies to bindable properties and only to those bindable properties that doesn't have another binding event specified.

Besides, you only really need to compute the hash twice (on save and on exit for comparison) whereas using events would be significantly more inefficient.

macke
Good idea. Thanks!
justin