9 Replies Latest reply on Jan 5, 2007 9:42 AM by mike_morearty

    accessing public variables

      I have the following code and I am not getting the correct value. You can see what I am trying to do. Any questions let me know. Thanks.

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

      <mx:script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      public var material1_ID: int;

      public function load_list():void{
      material1_ID = 0;
      }

      public function addRec():void{
      Alert.show('value before' + material1_ID.toString());
      insertService.addMaterial('123'); // THis calls my webservice and returns a query
      Alert.show('value after' + material1_ID.toString());
      }

      public function addMaterial(event:ResultEvent):void{
      material1_ID = 9;
      }
      ]]>
      </mx:Script>

      <mx:RemoteObject
      id ="insertService"
      destination="ColdFusion"
      source ="insert"
      >
      <mx:method name="addMaterial" result="addMaterial(event)" >
      </mx:RemoteObject>

      </mx:Application>
        • 1. accessing public variables
          187_2007 Level 1
          I'm not sure 100% what it is you are trying to do... But this is how I send Data to a CFC. When AddMaterial function gets invoked then you can display your data. I ran a query in the CFC and returned the data as an array. Just easier for me. I edited your code and commented it with the date and some notes. Let me know if this helps any:
          Re_LOOKING at this code... What is the returntype of your CFC?

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

          <mx:script>
          <![CDATA[
          import mx.controls.Alert;
          import mx.collections.ArrayCollection;
          import mx.rpc.events.ResultEvent;

          [Bindable]
          public var material1_ID: int;

          public function load_list():void{
          //material1_ID = 0;

          //Run the Remote Object and call the function
          insertService.addMaterial(); // you can send data to this function like this. insertService.addMaterial("dog") added 1/3/07
          }

          public function addRec():void{
          Alert.show('value before' + material1_ID.toString());
          insertService.addMaterial('123'); // THis calls my webservice and returns a query
          Alert.show('value after' + material1_ID.toString());
          }

          public function addMaterial(event:ResultEvent):void{
          material1_ID = 9;
          }

          ]]>
          </mx:Script>

          <!--
          <mx:RemoteObject id ="insertService" destination="ColdFusion" source ="insert">
          <mx:method name="addMaterial" result="addMaterial(event)" >
          </mx:RemoteObject>
          -->

          <mx:RemoteObject id="insertService" destination="ColdFusion" source="insert" result="addMaterial(event)"/> <!-- added 1/3/07 -->


          </mx:Application>
          1 person found this helpful
          • 2. Re: accessing public variables
            My cfc returns a query. My whole problem was that I could not get the public variable to hold its value.
            • 3. Re: accessing public variables
              187_2007 Level 1
              ahhhhh. okay. You can display the the object like this

              import mx.utils.ObjectUtil;

              trace(ObjectUtil.toString(event.result));

              I could not figure out how to grab the object so I could work with it's element. If you figure that out let me know on this thread. I made my returnValue an array and it was easier to work with that way. Chances are you will probably use a array or structures to package up your query data and send it back to the FLEX app. At least that is what I ma going to have to accomplish. Let me know if this works.
              • 4. Re: accessing public variables
                Thanks for the help. My problem was that I was setting the public variable material1_ID and it was not holding its value after the webservice call. Do you know why this could be? I set it when the call to the .cfc takes place. Right now I am just hardcoding a value of 9 and I can not get the material1_ID variable to print out nine after it calls the addMaterial function ( which takes place when the service is called)

                Also, what do you mean that you want to know how to work with its elements? I may be able to help you if you give me more of an explanation.
                • 5. Re: accessing public variables
                  187_2007 Level 1
                  hummmm.... First I would add this to your function. Better Syntex and it's faster plus it helps debugging.

                  public function addMaterial(event:ResultEvent):void{
                  var material1_ID:String = "9"; //If it is a String OR
                  var material1_ID:int = 9; //If it is an integer
                  }

                  Also, I would try and declairing the variable in a constructor or something, then populate it like I did above from the addMaterial function... See if that works.
                  • 6. Re: accessing public variables
                    ntsiii Level 3
                    But he wants that variable to be a bindable instance variable that he can set in the result handler.

                    If the instance variable is not holding its value then you are setting it somewhere you don't realize.

                    Are you using FB, where you can step through the code and watch that variable?

                    Can you reproduce the issue in a runnable sample app? Probably the exercise of doing that will show you where the provblem is anyway.

                    Tracy
                    1 person found this helpful
                    • 7. Re: accessing public variables
                      Yes I am using FB. I stepped through it and the variable is only getting set in the addMaterial function. It is almost like it is just local there. Anytime I refer to that variable outside the scope it is whatever my value is in the load_list function.
                      • 8. Re: accessing public variables
                        mike_morearty Level 1
                        Maybe I'm not reading your post carefully enough, but it seems to me that the problem is that you are assuming that Flex is synchronous -- that is, in these three lines of code from your original sample, you are assuming that the addMaterial() call *synchronously* contacts your web service, and blocks until the webservice has completed, so you are expecting that the 'value after' will be different than the 'value before':

                        Alert.show('value before' + material1_ID.toString());
                        insertService.addMaterial('123'); // THis calls my webservice and returns a query
                        Alert.show('value after' + material1_ID.toString());

                        That is an incorrect assumption. Flex's webservice calls are always asynchronous. That is, the above call to addMaterial() *initiates* the webservice call, but then returns immediately before the webservice call has been completed.

                        If you convert the Alert.show() statements to trace() statements, and add another trace() to function addMaterial(), and then let it run in the debugger but with no breakpoints, you'll see what I mean: The trace() in addMaterial() will, I think, show up *after* the 'value after' trace statement.

                        (By the way, in case you hadn't realized this yet, Alert.show() is also asynchronous! When you call it, that sets up Flex to show an alert, but the call returns immediately. trace() is synchronous in the sense that it immediately sends its message to the debugger, so it's easier to use trace() when figuring out asynchronous flow through the program.)

                        The way you deal with the asynchronous nature of web services is by using an event listener which will get triggered when the webservice is complete; that listener can then do whatever followup work is needed. In your case, the event listener is result="addMaterial(event)".

                        After all that, I hope I didn't entirely miss the point here :-)