1 Reply Latest reply on Apr 13, 2010 8:13 PM by archemedia

    Who called me? And how do I call them back?


      I have a simple issue, but in my attempt to run before I was fully walking I missed a step and fell.


      I created a dialog which has functionality I'll need elsewhere in my app, so I stripped all AS from the dialog component and placed it in a class outside of the default package. I now need to be able to update things on the component from within the class, what is the best approach to this? How can I decouple the design from the functionality?


      I can add event listeners in the component to listen for events generated by the class, but how would I do something like update a progress bar on a form?


      I guess the outerim question is: how does the class know who instantiated it?


      Thanks for any help,


        • 1. Re: Who called me? And how do I call them back?
          archemedia Level 4



          You can always pass an instance to a property of a class.


          Suppose you have a class MyClass with a public property 'owner', then within a component you could do something like:




          public var classInst:MyClass;


          private function buttonClickOrSomething():void


               classInst = new MyClass();

               classInst.owner = this;




          Off course, smart use of event listeners and event dispatching is always a better approach.


          The general idea of decoupling design from functionality is to use some sort of mvc pattern (model, view, controller).

          The model, which in most cases is a singleton class, keeps track of just data (like data coming from a server) and updates properties. It also dispatches events. These models don't know anything about components in your application.

          Now, the views, which are custom components use two main things to update themselves:

          - Event listeners

          - Data binding


          A very simple example would be that you have a singelton class called GlobalData which has a property userName.

          Now, if you have a custom component with a label configured like this:


          <mx:Label text="{GlobalData.getInstance().userName}"/>


          then the label will be updated automatically when the userName property changes. Another component could change the userName property of GlobalData and all 'binded' components will update automatically!


          Hope this helps