5 Replies Latest reply on May 22, 2009 10:03 AM by mirie00

    Binding Basic Questions

    mirie00

      Noob questions...

       

      Can you help me figure out why the following won't work for me?  I am so frustrated!

       

      //MyComponent.mxml

       

      <VBox:etc...>
      [Bindable]
      public var url:String;

       

      public function setAndCallService():void {
      url = "http://localhost:8888/blah/blah/blah";
      serv.send();
      }

       

      <mx:HTTPService id="serv"
                  url="{url}"
                  result="serv_result(event);"
                  resultFormat="e4x"
                  fault="serv_fault(event);" />

       

      private function serv_fault(evt:FaultEvent):void {
           trace("serv fault occurred: " + error.text);
           trace("url " + url);
           error.visible = true;
      }

       

      //... the rest of the file ...

       

      What occurs is that the serv.send() gets called, and serv_fault is then called and the first trace line indicates that no URL was given to the HTTPService component.  and yet the url gets printed out by the second trace line correctly as "http://....blah/blah".

       

      If you have any extra time, might you be able to explain why if I have [Bindable] set for a private property, why I also need [Bindable] by the getter/setter for that property (and which?  the getter or the setter?

       

      Thanks very much.

       

      In addition, if you've got time to explain any/all of the following...

       

      1.  I know this is probably very stupid, but if objects are passed by reference in flex, why do i need to bind them?  Aren't I always seeing the single instance (and its updates) wherever I have a reference to it (in various components to which i've passed the object)?

       

      2.  If i have a private property in a component, can a child component see this private property, or does it need to be made public?

       

      3.  If i set a private property to be [Bindable], why then would i also need to add [Bindable] to the getter/setter for that property?  And when should i set the getter to Bindable vs. the setter?

       

      Thanks again for all the clarification ahead of time.

        • 1. Re: Binding Basic Questions
          BennyBeta Level 4

          Binding happens asynchronously.

           

          The HTTPService's url property starts off as null (same as url).

          Then you set url = "http://...", which triggers a binding event.

          Then you serv.send(), which tries to make use of the HTTPService's url property (which is still not set).

          Then the binding event is handled which sets the HTTPService's url property.

           

          Does that help at all?

           

          Ben Edwards

          • 2. Re: Binding Basic Questions
            mirie00 Level 1

            Thanks so much.  So how then should I ensure that serv.send does not get called until after the HTTPService's url property has indeed been set?

            • 3. Re: Binding Basic Questions
              BennyBeta Level 4

              1.You don't always need to make use of binding. For instance, in your example, I don't think you need a separate url property and/or binding.  Just use the HTTPService's property directly.

               

              2. Other components will not be able to see private properties. So you could make it public or expose it via get/set functions.

               

              3. Because you are actually defining two different properties. The private property and the property defined by the get/set functions.
              You probably only need to make the public get/set functions bindable (not the private property). And it doesn't really matter if the [Bindable] is specified before the get/set (in most cases), because it applies to the property, not the functions.

               

              The following documentation may be helpful:

              http://www.flexafterdark.com/docs/Flex-Binding

               

              Let me know if that helped at all.

               

              Ben Edwards

              • 4. Re: Binding Basic Questions
                BennyBeta Level 4

                Just set the url directly on the HTTPService instance:

                 

                public function setAndCallService():void {
                   serv.url = "http://localhost:8888/blah/blah/blah";
                   serv.send();
                }

                 

                Does that help?

                 

                Ben Edwards

                1 person found this helpful
                • 5. Re: Binding Basic Questions
                  mirie00 Level 1

                  Wow, can't believe I looked right past that.  Duh!  Thanks very much.