25 Replies Latest reply on Feb 2, 2009 11:48 AM by rtalton

    changeWatcher of variable?

    David Repas
      Hey all - I'm trying to setup a changeWatcher to watch a variable, and then launch a function when that change occurs. I've been able to watch a textbox field, but not a variable directly.

      Here's my sample code that watches a textbox - the textbox id is "textboxField" - this works.

      public function initWatcher():void
      {
      ChangeWatcher.watch(textboxField, "text", watcherListener);
      }

      public function watcherListener(event:Event):void
      {
      openFunction();
      }

      Any ideas on what to do if I wanted to directly watch a variable named "testVar"?


      Thanks,
      David
        • 1. Re: changeWatcher of variable?
          rtalton Level 4
          Depends on the data type of the variable. You can bind to any public property of the data.
          • 2. Re: changeWatcher of variable?
            David Repas Level 1
            Let's say "testVar" is a String variable. I've tried the following, but this doesn't seem to work.

            ChangeWatcher.watch(testVar, "value", watcherListener);

            Any advice? Thanks.
            • 3. Re: changeWatcher of variable?
              rtalton Level 4
              Well we're in a pickle now. I looked into this and from what I see, we cannot bind to a variable that doesn't have a public property. The "String" class has no props -- well it has "length", but even that can't be bound, apparently. I don't know how you'd get the value of the var itself into the chain parameter of a ChangeWatcher. I used the canWatch method to test for different properties of 'testVar', and found nothing that was bindable.

              Let's just say THIS IS IMPOSSIBLE!!! That should get someone to find a solution!
              • 4. Re: changeWatcher of variable?
                Level 7

                "rtalton" <webforumsuser@macromedia.com> wrote in message
                news:gk8lbn$iga$1@forums.macromedia.com...
                > Well we're in a pickle now. I looked into this and from what I see, we
                > cannot
                > bind to a variable that doesn't have a public property. The "String" class
                > has
                > no props -- well it has "length", but even that can't be bound,
                > apparently. I
                > don't know how you'd get the value of the var itself into the chain
                > parameter
                > of a ChangeWatcher. I used the canWatch method to test for different
                > properties
                > of 'testVar', and found nothing that was bindable.
                >
                > Let's just say THIS IS IMPOSSIBLE!!! That should get someone to find a
                > solution!

                I would think you could use

                ChangeWatcher.watch(this, "testVar", watcherListener);

                I highly recommend anyone having binding problems to check out this
                presentation:
                http://tinyurl.com/databinding


                • 5. changeWatcher of variable?
                  rtalton Level 4
                  Great link for a lot of data binding info and background. Unfortunately, it never addresses binding to a primitive like a string or boolean variable which expose no named properties to pass to a ChangeWatcher instance.

                  Amy, I tried your suggestion "ChangeWatcher.watch(this, "testVar", watcherListener);" but had no success. Tried various other things to get a binding prop out of "tsetVar", but nothing worked.
                  On creationComplete of app I used the canWatch method to check if something was watchable:
                  var x:Boolean = ChangeWatcher.canWatch(this,"testVar"); FALSE
                  var x:Boolean = ChangeWatcher.canWatch(this,testVar); FALSE
                  var x:Boolean = ChangeWatcher.canWatch(this,testVar.valueOf()); FALSE

                  Of course, I had already tried passing testVar as the Object:
                  var x:Boolean = ChangeWatcher.canWatch(testVar,'length'); FALSE

                  Went through the String Class and saw nothing bindable using changeWatcher.

                  I haven't given up yet, but I think the answer may involve something not-so-obvious. Or maybe something obvious: "can't be done". See? I cover all the bases.
                  • 6. Re: changeWatcher of variable?
                    Level 7

                    "rtalton" <webforumsuser@macromedia.com> wrote in message
                    news:gkarfq$atq$1@forums.macromedia.com...
                    > Great link for lota of data binding info and background. Unfortunately, it
                    > never addresses binding to a primitive like a string or boolean variable
                    > which
                    > expose no named properties to pass to a ChangeWatcher instance.
                    >
                    > Amy, I tried your suggestion "ChangeWatcher.watch(this, "testVar",
                    > watcherListener);" but had no success. Tried various other things to get a
                    > binding prop out of "tsetVar", but nothing worked.
                    > On creationComplete of app I used the canWatch method to check if
                    > something
                    > was watchable:
                    > var x:Boolean = ChangeWatcher.canWatch(this,"testVar"); FALSE
                    > var x:Boolean = ChangeWatcher.canWatch(this,testVar); FALSE
                    > var x:Boolean = ChangeWatcher.canWatch(this,testVar.valueOf()); FALSE
                    >
                    > Of course, I had already tried passing testVar as the Object:
                    > var x:Boolean = ChangeWatcher.canWatch(testVar,'length'); FALSE
                    >
                    > Went through the String Class and saw nothing bindable using
                    > changeWatcher.
                    >
                    > I haven't given up yet, but I think the answer may involve something
                    > not-so-obvious. Or maybe something obvious: "can't be done". See? I cover
                    > all
                    > the bases.

                    Is the string variable actually bindable?


                    • 7. Re: changeWatcher of variable?
                      rtalton Level 4
                      Not that I can see, no.
                      • 8. Re: changeWatcher of variable?
                        John Hall Level 4
                        Couldn't you somehow wrap it in an ObjectProxy or something (convoluted I know). Something like
                        • 9. Re: changeWatcher of variable?
                          John Hall Level 4
                          Whoops. I see you're talking about Strings so change the example to two buttons:

                          <mx:Button label="Click Me" click="testVar.value = 'I am button one';"/>
                          <mx:Button label="Click Me" click="testVar.value = 'I am button two'"/>
                          • 10. Re: changeWatcher of variable?
                            John Hall Level 4
                            And not to beat a dead horse, but this link looks useful too.

                            http://www.rubenswieringa.com/blog/binding-read-only-accessors-in-flex

                            Guess I better move to a new thread ;-)
                            • 11. Re: changeWatcher of variable?
                              rtalton Level 4
                              Hey John, this looks interesting. I'll give it a try in the morning. Too dead right now. I had tried the object proxy route with no success. Did you actually get this sample to work?
                              • 12. Re: changeWatcher of variable?
                                David Repas Level 1
                                I think I have something here. I can't take credit for any of it - Mike Orth at the 615Flex Group gave me the following examples. I'm currently using the getter/setter example.


                                Getter/Setter example:

                                private var _testVar:String;
                                public function get testVar ():String
                                {
                                return _testVar;
                                }
                                public function set testVar (value:String):void
                                {
                                if (value != _testVar)
                                {
                                // use an if statement like this to make sure it only processes if the value changes
                                _testVar = value;

                                // DO LOGIC HERE...
                                testFunction();
                                }
                                }


                                Custom Binable Event example:

                                private var _testVar:String;
                                [Bindable(event="testVarChange")]
                                public function get testVar ():String
                                {
                                return _testVar;
                                }
                                public function set testVar (value:String):void
                                {
                                if (value != _testVar)
                                {
                                _testVar = value;
                                dispatchEvent (new Event ("testVarChange"));
                                }
                                }
                                ....
                                testVar.addEventListener("testVarChange", watcherListener);
                                • 13. Re: changeWatcher of variable?
                                  Level 7

                                  "rtalton" <webforumsuser@macromedia.com> wrote in message
                                  news:gkbsh1$ksc$1@forums.macromedia.com...
                                  > Not that I can see, no.

                                  If you're responding to me (sometimes the threading doesn't match between
                                  the nntp and forum sides, the variable _has_ to be bindable for you to be
                                  able to use a changeWatcher on it. Otherwise, the event that allows the
                                  changeWatcher to determine something has changed isn't created, as explained
                                  in the presentation I posted a link to earlier.

                                  HTH;

                                  Amy


                                  • 14. Re: changeWatcher of variable?
                                    Level 7

                                    "David Repas" <webforumsuser@macromedia.com> wrote in message
                                    news:gkc0hj$pkl$1@forums.macromedia.com...
                                    >I think I have something here. I can't take credit for any of it - Mike
                                    >Orth at
                                    > the 615Flex Group gave me the following examples. I'm currently using the
                                    > getter/setter example.

                                    Those guys are great, aren't they? :-)


                                    • 15. Re: changeWatcher of variable?
                                      rtalton Level 4
                                      @John Hall
                                      Pure genious. This works perfect. The proxied object can contain any values I want to assign: string, boolean, etc., and now a change watcher can be assigned to it and respond to changes in those values. I've been looking for an answer to this for about a month now! A huge "thank you" from the California coast.

                                      @Amy
                                      My threading will always match for you...

                                      @David Repas
                                      Did you try John's method yet?
                                      • 16. Re: changeWatcher of variable?
                                        David Repas Level 1
                                        Just tried the ObjectProxy version posted by John Hall - very cool - works perfectly. Many thanks for the example.


                                        David
                                        • 17. Re: changeWatcher of variable?
                                          John Hall Level 4
                                          Glad it worked. Thanks for all your (and Amy's) help in this forum.
                                          • 18. Re: changeWatcher of variable?
                                            Level 7

                                            "John Hall" <webforumsuser@macromedia.com> wrote in message
                                            news:gkeet6$sr5$1@forums.macromedia.com...
                                            > Glad it worked. Thanks for all your (and Amy's) help in this forum.

                                            You're welcome :)


                                            • 19. Re: changeWatcher of variable?
                                              John Hall Level 4
                                              Ever since Amy mentioned Michael's presentation on dataBinding, I've been meaning to go back to that presentation, which is pretty cool. Although making the object generic using objectProxy, as in

                                              private var testVar:ObjectProxy = new ObjectProxy();

                                              works fine and actually pretty cool in the way that it'll bind to almost any type of object, I've watched Michael's presentation again and I am understanding better the advise that David got. In short, using explicit setters and getters rather than relying upon implict getters and setters that come along with databinding. The only reason I question ObjectProxy is I can't help to wonder if it's not creating tons of unnecessary code to catch any value. I honestly don't know the answer and I'd love someone like the engineers or someone who really does deep dives into databinding to comment if using the following code is quicker in executing or not:

                                              <?xml version="1.0"?>
                                              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
                                              <mx:Script>
                                              <![CDATA[
                                              import mx.controls.Alert;
                                              private var _testVar:String;

                                              [Bindable]
                                              public function get testVar():String{
                                              return _testVar;
                                              }
                                              public function set testVar(value:String):void{
                                              _testVar = value;
                                              Alert.show("Detected change in testVar which is now " + testVar);
                                              }
                                              ]]>
                                              </mx:Script>
                                              <mx:Button label="Click Me" click="testVar = 'One'"/>
                                              <mx:Button label="Click Me" click="testVar = 'Two'"/>
                                              </mx:Application>
                                              • 20. changeWatcher of variable?
                                                John Hall Level 4
                                                And as David said, it's probably even lighterweight to use a custom event so it's not on a generic propertyChange event that everything in the world is listening to. OH, these mental sidetrips!

                                                PS: On top of that, I just learned how to edit an old post that I created so I don't need to keep stringing on afterthoughts. That's cool.
                                                • 21. Re: changeWatcher of variable?
                                                  Level 7

                                                  "John Hall" <webforumsuser@macromedia.com> wrote in message
                                                  news:gm5t8l$f5$1@forums.macromedia.com...
                                                  > Ever since Amy mentioned Michael's presentation on dataBinding, I've been
                                                  > meaning to go back to that presentation, which is pretty cool. Although
                                                  > making
                                                  > the object generic using objectProxy, as in
                                                  >
                                                  > private var testVar:ObjectProxy = new ObjectProxy();
                                                  >
                                                  > works fine and actually pretty cool in the way that it'll bind to almost
                                                  > any
                                                  > type of object, I've watched Michael's presentation again and I am
                                                  > understanding better the advise that David got. In short, using explicit
                                                  > setters and getters rather than relying upon implict getters and setters
                                                  > that
                                                  > come along with databinding. The only reason I question ObjectProxy is I
                                                  > can't
                                                  > help to wonder if it's not creating tons of unnecessary code to catch any
                                                  > value. I honestly don't know the answer and I'd love someone like the
                                                  > engineers
                                                  > or someone who really does deep dives into databinding to comment if using
                                                  > the
                                                  > following code is quicker in executing or not:

                                                  quicker than what?


                                                  • 22. changeWatcher of variable?
                                                    John Hall Level 4
                                                    Molasses maybe.

                                                    Actually, it would have absolutely no affect here, I'm sure. It's probably all on the same computer cycle. I was simply raising Michael's suggestion of not getting in the habit of always relying upon the simple propertyChange event as applications scale up because then every watcher in the program is tapped to see if it needs to handle the property change. I'm guessing few of us create applications that are large enough to matter one way or the other. However, I did enjoy going through the presentation a second time and learned more the second time through. Understanding more of the behind-the-scenes stuff is actually kind of fun.
                                                    • 23. Re: changeWatcher of variable?
                                                      rtalton Level 4
                                                      quote:

                                                      Originally posted by: John Hall
                                                      However, I did enjoy going through the presentation a second time and learned more the second time through. Understanding more of the behind-the-scenes stuff is actually kind of fun.


                                                      John, I think you've been working way too hard. Now, take a deep breath and repeat after me:
                                                      "Ease of use....ease of use..."
                                                      • 24. Re: changeWatcher of variable?
                                                        John Hall Level 4
                                                        lol, you win