4 Replies Latest reply on Jun 14, 2010 2:04 PM by _H_P_

    Unable to bind

    _H_P_

      Hello,

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"
               title="Monitor" xmlns:plcservicebean="server.services.plcservicebean.*">
          <fx:Script>
              <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.controls.Alert;
                  import mx.rpc.CallResponder;
                  import mx.rpc.events.FaultEvent;
                  import mx.rpc.events.ResultEvent;
                  
                  import server.valueObjects.Data;
                  
                  [Bindable] 
                  public var dbl:Number;
                  
                  
                  public function clientMonitor():void{
                      var callResp:CallResponder = new CallResponder();
                      callResp.addEventListener(ResultEvent.RESULT, monitorResult);
                      callResp.addEventListener(FaultEvent.FAULT, monitorFault);
                      callResp.token = plcServiceBean.getMonitorData();
                  }
                  
                  public function monitorResult(event:ResultEvent):void{
                      dbl = event.result.value as Number;
                  }
                  
                  protected function monitorFault(event:FaultEvent):void{
                      Alert.show(event.fault.faultString, "Error while monitoring Data ");
                  }
              ]]>
          </fx:Script>
          <s:layout>
              <s:VerticalLayout horizontalAlign="contentJustify" />
          </s:layout>
          
          <fx:Declarations>
              <plcservicebean:PlcServiceBean id = "plcServiceBean" 
                                             showBusyCursor="true" 
                                             fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"/>
          </fx:Declarations>
          <mx:Form height="103">
              <mx:FormItem label="View" x="2" y="11">
                  <s:TextInput id = "view1"
                               text="{dbl}"/>
              </mx:FormItem>
          </mx:Form>
      </s:Panel>

       

      Here the method clientMonitor() is called once on creationcomplete() from the main application.

       

      I am not able to bind the Bindable variable dbl to my textInput. Using the debugger, I am able to see that the result is assigned successfully to variable dbl, but it is not able to bind it to the text of view1. I see a NaN displayed in the text of view1.

       

      -H

        • 1. Re: Unable to bind
          David_F57 Level 5

          hi,

           

          You are trying to bind a number to a string, you need to type cast

           

          text="{String(dbl)}"

           

           

          David.

          • 2. Re: Unable to bind
            _H_P_ Level 1

            Hello David,

             

            If  I use

             

            text = "{String(dbl)}"

             

             

            a NaN is displayed. So, I added the following, but it is not helping either:

             

            [Bindable] public var val:String;
            [Bindable] public var dbl:Number;
            
            public function monitorResult (event:ResultEvent):void {
                 dbl = event.result.value as Number;
                 val = dbl.toString();
                 trace (dbl);
                 trace(val);
            }
            
            // ...code same as above..
            
            <mx:Form height="103">
            
                    <mx:FormItem label="View" x="2" y="11">
                        <s:TextInput id = "view1"
            
                                    text="{val}"/>
                   </mx:FormItem>
                </mx:Form>
            
            

             

            Here,  trace(dbl) and trace(val) gives me 100.00 which is the  desired value, but

             

            text = "{val}"

             

            does not display any thing, it remains blank.

             

            Please advise.

             

            -H

            • 3. Re: Unable to bind
              David_F57 Level 5

              hi,

               

              First thing,  you are able to bind its your typecasting that is cuasing a problem, the following code works fine because all the typecasting is correct, Now without knowing what your monitor event is returning its hard to say as casting something that may already be in error will still leave you with a NaN and that is happening way before the binding. In these instances trace is useless to you as it just returns a 'printable' object it doesn't care what the type is.

               

              In debug mode inspect your event.value to see what type it is...

               

              <?xml version="1.0" encoding="utf-8"?>

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                 xmlns:s="library://ns.adobe.com/flex/spark"

                 xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="monitorResult(120.01)">

               

              <fx:Declarations>

              <!-- Place non-visual elements (e.g., services, value objects) here -->

              </fx:Declarations>

              <fx:Script>

              <![CDATA[

              [Bindable] public var dbl:Number;

               

              public function monitorResult (v:Number):void {

              dbl = v;

              }

               

              ]]>

              </fx:Script>

              <s:TextInput id = "view1" text="{String(dbl)}"/>

              </s:Application>

              • 4. Re: Unable to bind
                _H_P_ Level 1

                Hello David,

                 

                My monitorResult() method is being called upon creationComplete. When a result event occurs, I am able to see  the

                dbl = event.result.value as Number;
                val = dbl.toString();
                
                

                value of val variable while in debug mode, but it does not get binded to the TextInput text.

                 

                I tried to remove the creation complete and invoke monitorResult() when a click event occurs on clicking a button. This, works fine, and I am able to bind the above val variable to the TextInput text.

                 

                Will you be able to suggest me why this may be happening, i.e. able to bind the result if a click event is generated and not on creationComplete.

                 

                Thanks,

                 

                -H