11 Replies Latest reply on Aug 5, 2008 6:53 PM by VarioPegged

    Adding an event listener for a completed task

    wkolcz Level 1
      Ok, here is the scenerio. Please look at what I have done and what am I missing.

      I have an component (LiveProjects.mxml) that has a datagrid that lists current projects. If you click button a pop up pops up (oddly enough) that allows you to enter a new project(addProject.mxml). When the user submits the project and it is inserted into the database, the pop up closes. I then need to prompt the parent to refresh the datagrid to show the new project.

      On my addProject I have

      <mx:Metadata>
      [Event(name="projectInserted", type="flash.events.Event")]
      </mx:Metadata>

      On the result of my RemoteObject I call the creation_result function:
      public function creation_Result(event:ResultEvent):void {
      Alert.show('Thank you! Click OK to return. Your project has been added to the database.','Confirmation');
      PopUpManager.removePopUp(this);
      dispatchEvent(new Event("projectInserted"));
      }

      What do I need to have on the parent to listed for that event and act on it. I thought I could add an event listener to the stage and have it act, but obviously that is wrong.

      stage.addEventListener(projectInserted, reloadProjects);

      Hint, help, links. Thanks.
        • 1. Re: Adding an event listener for a completed task
          VarioPegged Level 2
          I'm not sure I follow exactly what the parent-child relationship is in your scenario, but the first thing I'd suggest is to make the dispatched event bubble.

          dispatchEvent(new Event("projectInserted", true));

          TS
          • 2. Re: Adding an event listener for a completed task
            Gregory Lafrance Level 6
            As long as you are adding the data to the dataProvider of the DataGrid, and as long as you call myCollection.refresh(), I think the DG should automatically refresh.
            • 3. Adding an event listener for a completed task
              wkolcz Level 1
              @VarioPegged

              I'll go on a limb and say that that is correct, but how can I set up a listener on the parent component to make something happen after?

              Should I have an init function that registers the addEventListener("projectInserted", reloadProjects);? Should I register it to the stage?

              stage.addEventListener("projectInserted", reloadProjects);
              • 4. Re: Adding an event listener for a completed task
                wkolcz Level 1
                @Greg Lafrance

                So something like projectData.refresh();?
                • 5. Re: Adding an event listener for a completed task
                  wkolcz Level 1
                  Ok, here is my revised question. On the titlewindow pop up (child of the LiveProject.mxml I assume) I have:

                  <mx:Metadata>
                  [Event(name="projectInserted", type="flash.events.Event")]
                  </mx:Metadata>
                  and

                  public function creation_Result(event:ResultEvent):void {
                  Alert.show('Thank you! Click OK to return. Your project has been added to the database.','Confirmation');
                  dispatchEvent(new Event("projectInserted", true));
                  PopUpManager.removePopUp(this); }

                  On the liveProject.mxml I need to know what to put in an initApp() function to register the eventlistener and

                  I tried but it didn't work:
                  public function initApp():void {
                  projectGateway.list(); //gets the data for the project grid
                  taskGateway.list(); //get data for the task grid
                  stage.addEventListener("projectInserted", reloadProjects);
                  stage.addEventListener("taskInserted", reloadTasks)
                  }

                  public function reloadProjects():void {
                  //projectGateway.list();
                  Alert.show("The event handler for the projects works!");
                  ProjectData.refresh();
                  }
                  • 6. Re: Adding an event listener for a completed task
                    Gregory Lafrance Level 6
                    I assume ProjectData is the collection object. Call refresh right after the collection is changed and you should be all set.
                    • 7. Re: Adding an event listener for a completed task
                      wkolcz Level 1
                      @George

                      I guess I am not sure what you mean. The 'create project' pop up has to alert the parent that the data is inserted, right? How do I call the refresh on a pop up? Isn't that a child of the parent? Based on the code snippet above, where would I put a refresh?
                      • 8. Adding an event listener for a completed task
                        VarioPegged Level 2
                        Edited:

                        SystemManager maintains the popup, so I think this is where the 'problem' lies with the event propagation and parent-child relationship.

                        TS
                        • 9. Adding an event listener for a completed task
                          justria Level 1
                          In case you don't want to use Flex' event, you might give CNotificatonCenter a try, since it implements a global - non parent/child - notification pattern:

                          * Subscribe to an event:
                          quote:

                          CNotificationCenter.subscribe( 'modelChanged', updateBindings );

                          * Post an event:
                          quote:

                          CNotificationCenter.post( 'modelChanged' );

                          * And a starting point for your class:
                          quote:

                          package com.company.com
                          {
                          import flash.utils.Dictionary;

                          import mx.collections.ArrayCollection;
                          import mx.controls.Alert;

                          public class CNotificationCenter
                          {

                          private static var repository:Dictionary = new Dictionary();

                          static public function init():void
                          {


                          }

                          static public function subscribe( aSelector:String, method:* ):void {

                          init();
                          if ( null == repository[ aSelector ] ) {
                          repository[ aSelector ] = new ArrayCollection();
                          }

                          ArrayCollection( repository[ aSelector ] ).addItem( method );

                          }

                          static public function post( aSelector:String, data:* = null ):void {

                          init();

                          if ( null == repository[ aSelector ] ) {
                          repository[ aSelector ] = new ArrayCollection();
                          }

                          try {

                          var tmpAC:ArrayCollection = ArrayCollection( repository[ aSelector ] ) ;

                          for each( var fn:* in tmpAC ) {
                          fn();
                          }

                          } catch ( e:Error ) {
                          // Decide what to do, probably nothing at all.
                          // DEBUG: Alert.show( 'CNotificationCenter.post!' );
                          }

                          }

                          }
                          }

                          • 10. Re: Adding an event listener for a completed task
                            wkolcz Level 1
                            @justria

                            Wow, that looks awesome and looks like it will do the trick. Just wish I understood it..lol. I'll five it a try.
                            • 11. Re: Adding an event listener for a completed task
                              VarioPegged Level 2
                              Ok, I've had some time to look at your problem. In LiveProjects.mxml try the attached ...

                              BTW, please don't start two separate threads on the same topic. I am speaking for myself, but reading your other thread on this issue, I don't think I misunderstood what it is you want to do. One can't always offer the solution you're seeking on the spot due to time constraints we all have.

                              TS