9 Replies Latest reply on Jun 14, 2010 12:49 PM by lkenney2

    Passing Values from One Component to Another

    lkenney2 Level 1

      Because the data is used in multiple different areas, I need my code to work like this -- in the main application an event is fired, maybe due to a button maybe something else, and when that event is fired, data is passed to something like a singleton class. Then whatever elements are using that data, say a textbox, needs to update with that new value. I need to be able to do this however many time the event is fired while the application is running.

       

      Could somebody tell me how to do this, or point me to a resource?

       

      Thanks.

        • 1. Re: Passing Values from One Component to Another
          jsd99 Level 3

          You could create an Event subclass that can carry an object around.  I have one I made called EventWithObject.  It's very simple:

           

           

          package com.jsd {

               import flash.events.Event;

           

               public class EventWithObject extends Event {

                    public var data:Object;

           

                    public function EventWithObject(type:String, data:Object) {

                         this.data = data;

                         super(type, true, false);

                    }

               }

          }

           

           

          I put in defaults for bubbles and cancelable but you could set it up differently if you need to be able to change them.

           

          Now when you receive this event, you just look at event.data to find the object.

          • 2. Re: Passing Values from One Component to Another
            UbuntuPenguin Level 4

            Sounds like you need to use some kind of framework.  Of course if you like unmaintainable code you could go with the singleton.  You should check out the 2nd generation frameworks they have for Flex/AS3.  They are pretty handy.

            Silly coder , singletons are for noobs.

             

            Ubu

            • 3. Re: Passing Values from One Component to Another
              jsd99 Level 3

              UbuntuPenguin wrote:

               

              Sounds like you need to use some kind of framework.  Of course if you like unmaintainable code you could go with the singleton.  You should check out the 2nd generation frameworks they have for Flex/AS3.  They are pretty handy.

              Silly coder , singletons are for noobs.

               

              Ubu

               

               

              Why in the world would you want to put another framework on top of Flex, which is already a framework?  Just want things to be really slow?

               

              EventWithObject doesn't use any singletons.

               

              -jsd-

              • 4. Re: Passing Values from One Component to Another
                UbuntuPenguin Level 4

                I'll leave this here

                http://http://www.google.com/#hl=en&source=hp&q=singleton+anti+pattern&aq=0&aqi=g3g-m2&aql =&oq=singleton+anti&gs_rfai=CpH69DxEQTKidF4vkgASpkrGvCgAAAKoEBU_QqN_e&fp=dfdf66882bd03aae

                It was my understanding that global variables break encapsulation , but I understand some may not be

                so interested in that.  Not that they don't have a point in the entire universe , but they are easy to abuse.  I've seen the damage they can do up close and if you ever do , you will never be the same.  Not to mention people like spaghetti , so spaghetti code must be even better!

                 

                Second of all , experience != expertise.  That is self explanatory for anyone who has gone into any field , work or hobby and the world is full of examples.

                • 5. Re: Passing Values from One Component to Another
                  lkenney2 Level 1

                  Hi jsd99. Sorry It took me so long to get back to you, but I've been trying to work out how to pass the data. I have two questions for you based on the event method.

                   

                  1. What code do I have to add to my extended textArea so it receives the data an updates the text value? Right now I have this:

                   

                  package com.ascentify.components.text
                  {
                      import com.ascentify.events.EventWithObject;
                     
                      import mx.controls.TextArea;
                     
                      public class EventWithObject extends TextArea
                      {       
                          public function EventWithObject()
                          {
                              super();
                          }
                         
                          public function setText(e:EduSpeakEvent):void
                          {
                              text = e.param.data;
                          }
                      }
                  }

                   


                  2. My text area doesn't update when a button is clicked. How do I set my dispatch event? The following gives me a 1180:Call to a possibly undefined method dispatchEvent error:

                   

                  dispatchEvent(new EventWithObject(EventWithObject.DISPLAY_TEXT, {data:text}));

                   

                  Yes I have imported EventDispatcher and yes I have created the DISPLAY_TEXT string. Any ideas?

                  • 6. Re: Passing Values from One Component to Another
                    jsd99 Level 3

                    EventWithObject should extend Event, not TextArea.

                     

                    If all you want is to have text in a TextArea control update, you should just use [Bindable] variables.

                     

                     

                    <?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">

                     

                        <fx:Script>
                            <![CDATA[
                                [Bindable] private var myText:String = 'Initial';
                            ]]>
                        </fx:Script>
                        <s:VGroup>
                            <s:TextArea text="{myText}" />
                            <s:Button label="change text" click="myText='what up'"/>       
                        </s:VGroup>
                    </s:Application>

                     

                    Here's how I would do this:

                    Create a class for your data model, make the properties of it bindable.

                    When the Application starts up, create an instance of the data model object.  Pass this instance to all your various components.

                    Now if component A wants to change the text, it can set that property on the data model object.  Component B will update its display automatically as long as everything is set up with [Bindable].

                    • 7. Re: Passing Values from One Component to Another
                      lkenney2 Level 1

                      Hi jsd99.  EventWithObject does extend Event, I miss typed here. Sorry about that.

                       

                      As I said, I don't want to update with a button. I need the text to update when a function in the main class is called, such as onXmlComplete(e:Event) or postInitializedGUI(). When those events are called, I need to set all instances of the TextArea to a certain value, which is why I'm trying to rig it with an event.

                      • 8. Re: Passing Values from One Component to Another
                        jsd99 Level 3

                        lkenney2 wrote:

                         

                        Hi jsd99.  EventWithObject does extend Event, I miss typed here. Sorry about that.

                         

                        As I said, I don't want to update with a button. I need the text to update when a function in the main class is called, such as onXmlComplete(e:Event) or postInitializedGUI(). When those events are called, I need to set all instances of the TextArea to a certain value, which is why I'm trying to rig it with an event.

                        If you use a bindable string as the datasource for the textarea, then you just have to update that string and the textarea will update itself automatically. Doesn't matter if the function is in the same class as the component.  They just have to be referencing the same string.

                        • 9. Re: Passing Values from One Component to Another
                          lkenney2 Level 1

                          Thanks jsd. I answered this problem the same way I got the answer for other issue you helped me with, so I won't repost. Thanks again!