;

PDA

Click to See Complete Forum and Search --> : Help! How do I dispatch an event from one class to another?


Kenwio
08-20-2008, 12:46 PM
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:
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:
<?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!

neznein9
08-20-2008, 01:33 PM
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());
}

Kenwio
08-21-2008, 06:03 AM
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...:mad: :confused:

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

neznein9
08-21-2008, 09:45 AM
In that case you'll need to make a custom event class that holds a variable:

package classes.model {

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

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

public final class ResultEvent extends Event {

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

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

//--------------------------------------
// PUBLIC VARIABLES
//--------------------------------------

public var xmlData:XMLListCollection;
}
}

And plug that into the existing setup:

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());
}