7 Replies Latest reply on Sep 13, 2008 5:59 PM by SiHoop

    Passing data between functions

    SiHoop Level 1
      The fode below is intended to get information from a database and return the value stored in the property peopleColumn. How can I return the value back to the class constructor?
        • 1. Re: Passing data between functions
          Level 7

          "SiHoop" <webforumsuser@macromedia.com> wrote in message
          news:gacmns$c44$1@forums.macromedia.com...
          > The fode below is intended to get information from a database and return
          > the
          > value stored in the property peopleColumn. How can I return the value back
          > to
          > the class constructor?
          >
          > package{
          > import mx.collections.ArrayCollection;
          > import mx.rpc.events.ResultEvent;
          > import mx.rpc.http.HTTPService;
          >
          > public class GetNames{
          > public var peopleColumn:Array
          > private var service:HTTPService
          > public function GetNames(){
          > peopleColumn = ["11", "22", "33", "44", "55"]
          > var params:Object = new Object();
          > params.studentsClassListID = "66";
          > service = new HTTPService();
          > service.url =
          > " http://localhost/geothentic_admin_php/createStudentsList_XML.php";
          > service.method = "GET";
          > service.addEventListener("result", getNamesResultHandler);
          > //service.addEventListener("fault", httpFault);
          > service.send(params)
          >
          > }
          > public function getNamesResultHandler(event:ResultEvent ) :void {
          > peopleColumn = event.result.programs.newStudent ;
          > }
          > }
          > }
          > public function getNamesResultHandler(event:ResultEvent ) :void {
          > peopleColumn = event.result.programs.newStudent ;
          > }
          > }
          >

          I have a component that is intended to let you put in a path in the mxml tag
          and it comes back with an XMLListCollection. Maybe it will help point you
          in the right direction:

          package magnoliamultimedia

          /*
          This component is made available by Magnolia Multimedia
          http://www.magnoliamultimedia.com
          under a creative commons license
          http://creativecommons.org/licenses/by/3.0/

          You are free:
          to Share — to copy, distribute and transmit the work to Remix — to adapt the
          work
          Under the following conditions:
          Attribution. You must attribute the work in the manner specified by the
          author or
          licensor (but not in any way that suggests that they endorse you or your use
          of the work).

          Attribute this work:
          Leave this comment at the top of the file
          */

          {
          import flash.events.Event;
          import flash.net.URLLoader;
          import flash.net.URLRequest;

          import mx.collections.XMLListCollection;

          [Bindable]
          public class XMLLoader extends XMLListCollection
          {
          private var _request:URLRequest;
          private var _path:String;
          private var _urlLoader:URLLoader = new URLLoader;

          public function XMLLoader(source:XMLList = null)
          {
          super(source);
          }
          public function get path():String{
          return _path;
          }
          public function set path(path:String):void{
          _path = path;
          _request = new URLRequest(_path);
          _urlLoader.addEventListener(Event.COMPLETE, getURLResult, false, 0,
          true);
          _urlLoader.load(_request);
          }

          private function getURLResult(e:Event):void{
          var resultXML:XML = XML(_urlLoader.data);
          this.source = resultXML.children();
          this.refresh();
          _urlLoader.removeEventListener(Event.COMPLETE, getURLResult);
          }
          }
          }

          Usage:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical"
          xmlns:mm="magnoliamultimedia.*">
          <mm:XMLLoader id="helloSource" path="data.xml" />
          <mx:VBox horizontalAlign="center" verticalAlign="middle"
          backgroundColor="#FFFFFF">
          <mx:Repeater id="helloRepeater" dataProvider="{helloSource}">
          <mx:Label text="{helloRepeater.currentItem.text}" textAlign="center"
          fontSize="14"/>
          </mx:Repeater>
          </mx:VBox>

          </mx:Application>

          HTH;

          Amy


          • 2. Re: Passing data between functions
            SiHoop Level 1
            Below, I've tried to simplify my problem. I have an mxml file and a class.

            The mxml file creates an instance of GetNames and traces GetNames. peopleColumn. However, peopleColumn is set twice in the class GetNames. First, it is given the value ["11", "22", "33", "44", "55"], but then when a php file has loaded it is changed to ["00", "00", "00", "00", "00"]. I don't understand why the changed value does not appear in the trace statement. Instead of tracing the changed value, it shows the original value.
            • 3. Re: Passing data between functions
              Level 7

              "SiHoop" <webforumsuser@macromedia.com> wrote in message
              news:gae40o$1ah$1@forums.macromedia.com...
              > Below, I've tried to simplify my problem. I have an mxml file and a class.
              >
              > The mxml file creates an instance of GetNames and traces GetNames.
              > peopleColumn. However, peopleColumn is set twice in the class GetNames.
              > First,
              > it is given the value ["11", "22", "33", "44", "55"], but then when a php
              > file
              > has loaded it is changed to ["00", "00", "00", "00", "00"]. I don't
              > understand
              > why the changed value does not appear in the trace statement. Instead of
              > tracing the changed value, it shows the original value.
              >
              > <?xml version="1.0" encoding="utf-8"?>
              > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
              > creationComplete="init()" layout="absolute" width="100%" height="100%">
              > <mx:Script>
              > <![CDATA[
              > import GetNames
              > private function init():void {
              > var myNames:GetNames = new GetNames()
              > var names:Array=myNames.peopleColumn
              > trace("Name="+names)
              > }
              > ]]>
              > </mx:Script>
              >
              > </mx:Application>
              >
              > Class file:
              > package{
              > import mx.rpc.events.ResultEvent;
              > import mx.rpc.http.HTTPService;
              >
              > public class GetNames{
              > public var peopleColumn:Array
              > private var service:HTTPService
              > private var returnVal:Array
              >
              > public function GetNames(){
              > peopleColumn = ["11", "22", "33", "44", "55"]
              > var params:Object = new Object();
              > params.studentsClassListID = "66";
              > service = new HTTPService();
              > service.url =
              > " http://localhost/geothentic_admin_php/createStudentsList_XML.php";
              > service.method = "GET";
              > service.addEventListener("result", getNamesResultHandler);
              > service.send(params)
              >
              > }
              > public function getNamesResultHandler(event:ResultEvent):Array {
              > peopleColumn = ["00", "00", "00", "00", "00"]
              > return peopleColumn
              > }
              > }
              > }

              peopleColumn is an Array type, so there is no way to tell that there was a
              change to it, and I don't see you firing any change events. The reason I
              used XMLListCollection as the base to my object and not URLLoader is so that
              all the event handling would be handled by the base class. Try using
              ArrayCollection as your base class and compositing the HTTPService to it.

              HTH;

              Amy


              • 4. Re: Passing data between functions
                SiHoop Level 1
                Amy,,
                Do you mean that I should should define 'names' an ArrayCollection instead of an Array in both the mxml and as files? If that's the case, my class would be as below. I did this, but the returned value was null.
                package{
                import mx.rpc.events.ResultEvent;
                import mx.rpc.http.HTTPService;
                import mx.collections.ArrayCollection
                public class GetNames{
                public var peopleColumn:ArrayCollection
                private var service:HTTPService
                private var returnVal:Array

                public function GetNames(){
                peopleColumn = ["11", "22", "33", "44", "55"] as ArrayCollection
                var params:Object = new Object();
                params.studentsClassListID = "66";
                service = new HTTPService();
                service.url = " http://localhost/geothentic_admin_php/createStudentsList_XML.php";
                service.method = "GET";
                service.addEventListener("result", getNamesResultHandler);
                service.send(params)

                }
                public function getNamesResultHandler(event:ResultEvent):ArrayCollection {
                peopleColumn = ["00", "00", "00", "00", "00"] as ArrayCollection
                return peopleColumn
                }
                }
                }
                • 5. Re: Passing data between functions
                  Level 7

                  "SiHoop" <webforumsuser@macromedia.com> wrote in message
                  news:gaek4p$ijv$1@forums.macromedia.com...
                  > Amy,,
                  > Do you mean that I should should define 'names' an ArrayCollection
                  > instead of
                  > an Array in both the mxml and as files? If that's the case, my class would
                  > be
                  > as below. I did this, but the returned value was null.
                  > package{
                  > import mx.rpc.events.ResultEvent;
                  > import mx.rpc.http.HTTPService;
                  > import mx.collections.ArrayCollection
                  > public class GetNames{
                  > public var peopleColumn:ArrayCollection
                  > private var service:HTTPService
                  > private var returnVal:Array
                  >
                  > public function GetNames(){
                  > peopleColumn = ["11", "22", "33", "44", "55"] as ArrayCollection
                  > var params:Object = new Object();
                  > params.studentsClassListID = "66";
                  > service = new HTTPService();
                  > service.url =
                  > " http://localhost/geothentic_admin_php/createStudentsList_XML.php";
                  > service.method = "GET";
                  > service.addEventListener("result", getNamesResultHandler);
                  > service.send(params)
                  >
                  > }
                  > public function getNamesResultHandler(event:ResultEvent):ArrayCollection
                  > {
                  > peopleColumn = ["00", "00", "00", "00", "00"] as ArrayCollection
                  > return peopleColumn
                  > }
                  > }
                  > }

                  I think your biggest problem is that GetNames is an object and can't
                  dispatch any events on its own. So you need to start out with (extend) a
                  class that CAN dispatch events. Since the ultimate format that you want is
                  an ArrayCollection, it makes sense to extend THAT instead of Object, and
                  just give your ArrayCollection a private HTTPService. I think it's beyond
                  the scope of this forum for me to teach you OOP, especially when I have
                  already given you dead simple example code that you can examine to see how
                  it works and explained (twice now) why I took the approach I did and how you
                  can modify your code to do what you want.

                  I also think that

                  peopleColumn = ["11", "22", "33", "44", "55"] as ArrayCollection

                  is going to give you null, because ["11", "22", "33", "44", "55"] is NOT an
                  ArrayCollection. If you want to keep banging your head against the same
                  brick wall despite my best efforts, try:

                  peopleColumn = New ArrayCollection(["11", "22", "33", "44", "55"] )

                  I hope you find what you are looking for.

                  -Amy



                  • 6. Re: Passing data between functions
                    wyattbiker Level 1
                    quote:

                    Originally posted by: SiHoop
                    Below, I've tried to simplify my problem. I have an mxml file and a class.

                    The mxml file creates an instance of GetNames and traces GetNames. peopleColumn. However, peopleColumn is set twice in the class GetNames. First, it is given the value ["11", "22", "33", "44", "55"], but then when a php file has loaded it is changed to ["00", "00", "00", "00", "00"]. I don't understand why the changed value does not appear in the trace statement. Instead of tracing the changed value, it shows the original value.



                    It is working however you are just seeing the result before service.send() request completes.

                    Try this MXML see what I mean:


                    • 7. Re: Passing data between functions
                      SiHoop Level 1
                      wyattbiker,
                      Thanks so much for pointing this out. I completely understand the flow of information now and, of course, it makes perfect sense!