0
votes

In Flex (Flash Builder 4), I need a way to know if something in an array collection has changed.

I have a custom object:

[Bindable] public var _myobject:MyObject;

It's basically just a class containing a bunch of different String and Number properties.

I need a reliable way to know if any of the properties have been changed. For example, I am binding the properties to a user interface (fields), but it's also possible for some of the properties to change through code.

Is there a way to do this? I found ChangeWatcher, but that looks like it only looks at a single simple property, such as a String or Number. I need to watch or detect changes in all the properties in my object, hopefully without having to add ChangeWatcher events to every property. Is this possible?

3
if there are lot of properties of objects that can change through code, you are better off manually kicking change events whenever you change anything. - Kamal
So everywhere that I make changed in code, I have to add event firing code... What about a screen with all properties bound to fields, do I have to add onChange events to each field, or is there not an easier way to detect that my object has changed when a property is changed in my object? - Scott Szretter
well.. have a little workaround.. which ll reduce the effort for you atleast to some extent.. assuming your object properties are not dynamic.. create an object with manually writing setters for all properties.. and any call to setter call a common function which ll dispatch an event for whatever property got changed.. that's how property change listeners work in flex.(if m not wrong, they do the setup during compile time) so even if object is tied to fields on screen you don't need to worry about changes.. - Kamal

3 Answers

1
votes

You're probably better off just dispatching binding events on the specific properties you want bindable. Better yet, dispatch a custom binding event, so that all of the things that are bound don't have to filter for "is this the property I care about?" It's really easy with Flash Builder 4.5 to do this, just select your variable name and press Ctrl-1, select "Create getter and setter," select getter and setter and check "Bindable" and "create custom event."

This will create code for you that looks something like this:

private var _yourProperty:String;

[Bindable (event='yourPropertyChange')]
public function get yourProperty():String {
   return _yourProperty;
}

public function set yourProperty(value:String):void {
   if (value !=== _yourProperty) {
     _yourProperty = value;
     dispatchEvent(new Event('yourPropertyChange'));
   }
}

This will be much less verbose and more performant than the code that Flash Builder generates for you behind the scenes when you just use the Bindable tag on its own.

0
votes

If you use defined classes as VO/DAO and apply the [Bindable] tag to the class, this will do binding on all properties for you (so long as they are read/write).

Anonymous object detection is difficult at best, let alone adding additional headaches of loosing compiler type checking.

Super basic example: - the key is to tie it to the dispatcher, so internally it can send out the PropertyChangeEvent.

 [Bindable]
 public class Actor extends EventDispatcher 
 {
      public var fName:String;
      public var lName:String;
      public var age:uint;

      public function get displayName():String
      {
          return lName +', '+ fName;
      }

      public function Actor()
      {
          super();
      }
 }
0
votes
public class BindableDictionary extends EventDispatcher {
        public function BindableDictionary() {
            super();
        }

        public var dictionary:Dictionary = new Dictionary();

        [Bindable("change")]
        public function get(key:Object):Object {
            return dictionary[key];
        }

        public function put(key:Object, value:Object):void {
            dictionary[key] = value;
            dispatchEvent(new Event(Event.CHANGE));
        }
    }

maybe this class will give you some new idea