2 Replies Latest reply on Apr 26, 2007 7:50 AM by RianFowler

    Event Bubbling: relaying custom events

    RianFowler Level 1
      Background:
      The event bubbling in action script 3 causes an event to travel from the initiating object through all of its parent objects to the root application. The event then travels back down to the initiating object.

      Goal: To keep the data, controller and views loosely coupled
      1.When view objects are instantiated, they send an event saying they need some information. The event has the type of information they need.
      2.The data object captures this event, determines if it has it in memory. If it doesn't, it retrieves the information from a web service and pushes the result on an array.
      3. Once the result is in memory, it shoots an event containing the array index of the result.
      4.The view captures the result, pulls out the index and uses it to bind to the correct array item

      Result: The view is automatically updated anytime the data object is called by the web service with an update.

      In this scenario neither the view nor data can be parents of each other but each needs to be able to respond to each other's events. The reason is so that I can have several dynamically generated views tapping into the same data.

      Issue:
      If the Data (Model) and Views are children of the same application, how does one maintain loose coupling between them?

      Current solution:
      The only thing I've been able to come up with is having the application or controller object catch all of the custom events and then re-dispatch them to the children. The problem with this is that it's messy.

      I could also design an event manager class that would re-dispatch events to the right hierarchy of children. I could include a private string variable that would capture the initiating object name when my custom event was called so the event manager class could do dynamic mapping.

      Is there a better solution?
        • 1. Re: Event Bubbling: relaying custom events
          buabco Level 1
          OK I'm not expert but I do have an idea.

          Ok, you can have your main application to listen to ADD and REMOVE events so it will know when a new component is added. You can also make a "register interface" to the components that will be registering to listen to your main data provider.

          So when your main application in the add event receives a target of the kind "register interface" it can add the apropiate listeners to the data provider and the component. Since the component is implementing your "register interface" you will know to witch function add the listener in the component.


          • 2. Re: Event Bubbling: relaying custom events
            RianFowler Level 1
            Thanks- I posted the question on something awful and someone suggested that I use a micro architecture recommended on Adobe Labs called Cairngorm I'm going to go through the documentation and give it a test drive this week.