6 Replies Latest reply on Nov 12, 2007 9:07 AM by LaffenFx

    Data binding does not detect changes

    LaffenFx
      When passing in bindable property to function and assigning a new object data binding breaks
      Have a look at the following simple code.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">

      <mx:VBox>
      <mx:Text id="textbox" text="{user.name}" />
      <mx:Button label="Update user" click="updateUser(user)" />
      <mx:Button label="Update user2" click="updateUser2()" />
      </mx:VBox>
      <mx:Script>
      <![CDATA[
      [Bindable]
      private var user:User;

      private function init():void
      {
      user = new User();
      user.name = "first name";
      }

      private function updateUser(newUser:User):void
      {
      newUser = new User();
      newUser.name = "new name";
      }
      private function updateUser2():void
      {
      user = new User();
      user.name = "new name2";
      }
      ]]>
      </mx:Script>
      </mx:Application>

      File, User.as
      package
      {
      [Bindable]
      public class User
      {
      public var name:String;
      }
      }

      Why does the function updateUser break the dataBinding?
      The function updateUser2 works
        • 1. Re: Data binding does not detect changes
          TeotiGraphix Level 3
          You are breaking the binding reference to the original User instance in your code.

          private function updateUser(newUser:User):void
          {
          newUser = new User(); // <- This is breaking the bindings
          newUser.name = "new name";
          }

          Peace, Mike
          • 2. Re: Data binding does not detect changes
            levancho Level 3
            I think binding is per instance based (correct me if I am wrong) and as it was pointed, you killing a bindable instance,

            for example :

            private function init():void
            {
            user = new User();// lets say this objects' memory address is : 0x010101
            user.name = "first name";
            }

            now : when you setup bidnings flex watches modification for object whose memory address is 0x010101, so if you null that object(there is no reference to it ) it appears that binding breaks and no rebinding happens for new instance.



            • 3. Re: Data binding does not detect changes
              LaffenFx Level 1
              Then I am still wondering:

              1. Why does the creation of the new instance in function updateUser2 work?
              (Because it reference the class variable directly..?)

              2. How can you change the function updateUser to not break the binding, but not change it to reference the class variable user directly?

              With this simple example I try to understand a data binding defect in a more complex project ;)
              • 4. Re: Data binding does not detect changes
                levancho Level 3
                hmm good question,

                (speculating) maybe dispatching event inside the function

                private function updateUser(newUser:User):void
                {
                newUser = new User();
                newUser.name = "new name";
                dispatchEvent(new Event("propertyChange"));
                }
                • 5. Re: Data binding does not detect changes
                  Karl_Sigiscar_1971 Level 3

                  Hi,

                  Our team had a similar problem on a project where we would update the model from a XML stream and have some components bind to the model. Some members bound to were ArrayCollections and they were re-created everytime a new stream would come, which was not working with data binding. The fix was to update the content of the ArrayCollection members as opposed to re-creating them.

                  Cheers,
                  Karl.
                  • 6. Re: Data binding does not detect changes
                    LaffenFx Level 1
                    Thanks for all replies;)

                    I think levancho's suggestion on sending out an event is good.

                    This could look something like this:

                    var userEvent:UserEvent = new UserEvent("New User", newUser);
                    dispatchEvent(userEvent);

                    Where UserEvent is a Event holding the new Object.
                    Then all components needing this change could, do this:

                    addEventListener("New User",userChanged);

                    private function userChanged(evt:UserEvent):void
                    {
                    evt.user; //Update binding with the new user...
                    }

                    After looking more into the problem I found:
                    The main problem initially was that the variable sent into the function will not update the refence sent into the function when creating a new instance, regardless of using data binding or not...