A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Help! How do I dispatch an event from one class to another?

  1. #1
    Member
    Join Date
    Jul 2008
    Posts
    30

    Help! How do I dispatch an event from one class to another?

    Hi!

    I want to dispatch an event from the MODEL(Stations) to the VIEW, when the Stations.onResult() function has been executed.

    How do I do this??

    This is the MODEL:
    Code:
    package classes.model {
        import mx.rpc.events.ResultEvent;
        import mx.rpc.http.HTTPService;
        import XMLList;
        import mx.collections.XMLListCollection;
        import flash.system.Security;
        
        public class Stations {
            
            private var _httpService:HTTPService = new HTTPService();
            private var _xmlData:XMLList = new XMLList();
            [Bindable]
            private var _xmlDataCollection:XMLListCollection;
            
            
            public function Stations():void {
                _httpService.url = "http://localhost/te.php";
                _httpService.useProxy = false;
                _httpService.method = "POST";
                _httpService.resultFormat = "e4x";
                _httpService.addEventListener(ResultEvent.RESULT, onResult)
                _httpService.send();
    
            }
            
            public function onResult(event:ResultEvent):void {
                _xmlData = event.result.fields.field;
                _xmlDataCollection = new XMLListCollection(_xmlData);
            }
            
            public function getTheData():XMLListCollection {
                return _xmlDataCollection;
            }
        }
    }
    This is the VIEW:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">
    
        <mx:Script>
            <![CDATA[
                import classes.model.Stations;
                import mx.collections.XMLListCollection;
    
                [Bindable]
                private var stationData:Stations = new Stations();
                
                private function init():void {
                    trace (stationData.getTheData());
                }
            ]]>
        </mx:Script>
        <mx:VBox>
            <mx:DataGrid id="stationGrid" dataProvider="{stationData.getTheData()}" />
        </mx:VBox>
    </mx:Panel>
    Please help!

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    PHP Code:
    public function onResult(event:ResultEvent):void {
        
    _xmlData event.result.fields.field;
        
    _xmlDataCollection = new XMLListCollection(_xmlData);
        
    dispatchEvent(new Event('onModelResult'));
    }


    // ...

    private function init():void {
        
    stationData.addEventListener('onModelResult'someFunction);
        
    trace (stationData.getTheData());


  3. #3
    Member
    Join Date
    Jul 2008
    Posts
    30
    Quote Originally Posted by neznein9
    PHP Code:
    public function onResult(event:ResultEvent):void {
        
    _xmlData event.result.fields.field;
        
    _xmlDataCollection = new XMLListCollection(_xmlData);
        
    dispatchEvent(new Event('onModelResult'));
    }


    // ...

    private function init():void {
        
    stationData.addEventListener('onModelResult'someFunction);
        
    trace (stationData.getTheData());

    Thanks! But it does not work for me though..

    What I need here is the _xmlDataCollection to be filled with the data from the HTTPService, and THEN be passed to the VIEW. I thought that if I'd dispatch an event from the onResult() function, that the the _xmlDataCollecition would be filled with the data...but I was wrong..instead it gives me an
    " Cannot access a property or method of a null object reference" error...

    Could someone just give me a code sample on HOW TO DISPATCH AN XMLLISTCOLLECTION FILLED WITH DATA FROM A HTTPSERVIDE TO ANOTHER CLASS??

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    In that case you'll need to make a custom event class that holds a variable:

    PHP Code:
    package classes.model {

         
    //--------------------------------------
        // PACKAGES
        //--------------------------------------

            
    import flash.events.Event;
            
    import mx.collections.XMLListCollection;

        public final class 
    ResultEvent extends Event {

            
    //--------------------------------------
            //  CONSTRUCTOR
            //--------------------------------------

                
    public function ResultEvent(s:Stringxx:XMLListCollection){
                    
    this.xmlData xx;
                    
    super(s);
                }

            
    //--------------------------------------
            //  PUBLIC VARIABLES
            //--------------------------------------
            
                
    public var xmlData:XMLListCollection;
        }

    And plug that into the existing setup:

    PHP Code:
    public function onResult(event:ResultEvent):void 
        
    _xmlData event.result.fields.field
        
    _xmlDataCollection = new XMLListCollection(_xmlData); 
        
    dispatchEvent(new ResultEvent('onModelResult'_xmlDataCollection));



    // ... 

    private function init():void 
        
    stationData.addEventListener('onModelResult', function(evt:ResultEvent):void{
            
    trace('Result Received: ' evt.xmlData);
        });

        
    trace (stationData.getTheData()); 


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center