12 Replies Latest reply on Jul 31, 2008 8:49 AM by VarioPegged

    Assigning Event Handlers

    karoljouis
      Hi. In my project i create an instance of a custom MXML component using actionscript and add it as a child of a tab navigator. is there any way to add an event handler to it.

      For example we can use normally <com:CustomVBox login="login(event)"/>

      But i don't know how to add the login(event) to the component instance at ru time like this

      var myvbox:VBox = new VBox();
      tab_bar.addChild(myvbox);


      Thanks in advance
        • 1. Re: Assigning Event Handlers
          Level 7

          "rocknrollcar" <webforumsuser@macromedia.com> wrote in message
          news:g6rj96$pkd$1@forums.macromedia.com...
          > Hi. In my project i create an instance of a custom MXML component using
          > actionscript and add it as a child of a tab navigator. is there any way to
          > add
          > an event handler to it.
          >
          > For example we can use normally <com:CustomVBox login="login(event)"/>
          >
          > But i don't know how to add the login(event) to the component instance at
          > ru
          > time like this
          >
          > var myvbox:VBox = new VBox();
          > tab_bar.addChild(myvbox);

          http://livedocs.adobe.com/flex/3/html/help.html?content=metadata_2.html


          • 2. Re: Assigning Event Handlers
            karoljouis Level 1
            Thank you.

            My application does the features like listing tasks,showing tasks, etc.I have a tabNavigator. i have custom components for ListTasks, NewTask and TaskShow. I add them to the tabNavigator using the addChild method. I list all the tasks in a datagrid in the ListsTasks component. On double clicking a dataItem, it should fetch the particular task from the server and open a TaskShow component in a new tab and display the task details there. I use a <mx:HTTPService> component to do that

            <mx:HTTPService id="showTheTask" url="/tasks/show/{tasksGrid.selectedItem.id}.xml" resultFormat="e4x" result="handleTaskShowResult(event)"/>


            The handler for the httpservice is

            private function handleTaskShowResult(event:ResultEvent):void
            {
            var result:Object = event.result;
            var currenttask:TaskShow = new TaskShow();
            parentDocument.addChild(currenttask);
            parentDocument.selectedChild = currenttask;
            dispatchEvent(new TaskShowEvent(XML(result)));
            }


            My custom event class is TaskShowEvent.as

            package com.events {
            import flash.events.Event;
            public class TaskShowEvent extends Event {
            public static const TASK_SHOW_EVENT:String = "taskShowEvent";
            public var task:XML;
            public function TaskShowEvent(task:XML) {
            super(TASK_SHOW_EVENT,true);
            this.task = task;
            }
            }
            }


            The import statements are correct for events as well as components. I have also added the metadata.The TaskShow component is also opening in the tab properly. Now i need how to get the task data such as task.name, task.description, task.created_on, etc and display in the TaskShow component. Here I don't know how to add a event listener for handling the incoming task XML data.


            I am a newbie. It would be great if u solve this issue.

            Thanks
            • 3. Re: Assigning Event Handlers
              VarioPegged Level 2
              Are there any other elements that'll be using the XML info that you're passing via the TaskShowEvent? If not, couldn't you assign the XML to the TaskShow instance in handleTaskShowResult().

              You could also try something like the following that'll allow you to circumvent the parentDocument.addChild() stuff...

              TS
              • 4. Re: Assigning Event Handlers
                karoljouis Level 1
                Thanks for the reply. Only the TaskShow component will use the XML fetched from the server. If i assign the XML to the TaskShow instance in handleTaskShowResult(), how would i refer to the task data and display in the TaskShow component.


                I will check out your code for the second method. Thanks a lot
                • 5. Assigning Event Handlers
                  karoljouis Level 1
                  Your second method is working. Thanks Vario. But please check out for my doubt in the previous post.
                  • 6. Re: Assigning Event Handlers
                    VarioPegged Level 2
                    You're welcome.

                    As for assigning your XML directly in the result handler, you can probably do the same type of thing ...

                    TS
                    • 7. Re: Assigning Event Handlers
                      karoljouis Level 1
                      I got a problem. Only currenttask.name = result.name;

                      is working properly and its shown in the TaskSHow view.

                      currenttask.description,etc are not working
                      • 8. Re: Assigning Event Handlers
                        VarioPegged Level 2
                        What does your XML schema look like?

                        TS
                        • 9. Re: Assigning Event Handlers
                          karoljouis Level 1
                          My XMl schema is
                          <task>
                          <id></id>
                          <name></name>
                          <description></description>
                          </task>

                          I got it working. What i did was i declared variables for each attribute of the task in my TaskShow component as public. Now they are displayed correctly.

                          Is my method correct?
                          • 10. Re: Assigning Event Handlers
                            VarioPegged Level 2
                            Yes, you could do it that way.

                            In general I would advocate using the second method I showed you, because you have a better separation of duties within the different methods (functions). I get my data in the result handler and then dispatch any additional event(s) to the app to let the listeners know the data is ready. Then let the listeners handle that data.

                            TS
                            • 11. Re: Assigning Event Handlers
                              karoljouis Level 1
                              Thanks Vario for your help
                              • 12. Re: Assigning Event Handlers
                                VarioPegged Level 2
                                No problem.

                                TS