3 Replies Latest reply on Jan 30, 2008 11:19 AM by BIOSMonkey

    Bindsetter for dynamically created objects

      If you have multiple instances of dynamically created objects, how can I use a bindsetter to attach a function that will execute for each instance upon the targeted property change?

      In case that didn't make sense, here is an example:

      Let's say I create ten Labels with Actionscript in a loop, and I want the text of the label to be some calculated value based on the value of a slider bar.

      I can do this:
      where xx is each Label instance as it is being instantiated. In this case, each Label instance will receive the changed value of slidVal.

      I am trying to achieve the same thing, but need to do some calculation of the monitored value before assigning it to xx.value. So I am trying to figure out how to use BindSetter to execute a function on the source property change. The problem is that I don't see how to attach a bindsetter function specifically to each Label instance.

      BindingUtils.bindSetter() only takes a setter function and source object and property, so there is no association with any particular target.

      How can I do this?

        • 1. Re: Bindsetter for dynamically created objects
          Hi!, i just would like to know if you have solved the problem with the binding. At this moment, i have the same problem, exactly how you did explain it.

          Please, if you could help me, i will apreciate it!
          • 2. Re: Bindsetter for dynamically created objects
            mariflux2 Level 1
            Hi.. could you solve the problem with the bindsetter???
            I still have problem with that and i must solve as soon as posible...
            • 3. Re: Bindsetter for dynamically created objects
              BIOSMonkey Level 1
              Yes, I believe so. It involves a slight modification of the bindproperty static function to take an additional parameter (your function). Data changes in the source are funneled into the passed function and then to the target property. Disclaimer: I do not know if this code is 100% the best way to do this, but it seems to work for me in my initial testing. I have NOT tested this completely! Give it a try though.

              internal function bindPropertywithFunction(
              site:Object, prop:String,
              host:Object, chain:Object,
              commitOnly:Boolean = false,
              var w:ChangeWatcher =
              ChangeWatcher.watch(host, chain, null, commitOnly);

              if (w != null)
              var assign:Function = function(event:*):void
              if (myfunc!=null)

              site[prop] = w.getValue();
              assign(null); // Initialize value in target thru myfunc

              return w;

              and you would use it by creating a changewatcher for each instance of your control:

              bindPropertywithFunction(xx,"value",slidVal,"value", false,
              // Perform some function on the value before assigning
              // it to the target
              return (Math.round(val+Math.random()*10));

              Note that I added the function after the commitOnly parameter to maintain consistency. If you do not use a function then the above code should act the same as bindProperty.

              Also, even though this does what I wanted, I am not sure how efficient it is to create a changewatcher for each instance of a control. If we assume that we create controls in a loop then perhaps the bindSetter would work if we kept an external array of references to the control instance list. (This is, of course, assuming that all controls in the loop would pass through the same kind of transformation). In other words, as each control is instantiated, I push its instance to an array, and then in the bindSetter function, use that array to take the changed value, modify it, and pass to each instance in the array. Just another idea.

              However, adding a unique changewatcher is useful if each control's value is dependent on some other value as determined during the instantiation loop.