Jump to content
GreenSock

scotbord

Extending LoaderItem

Recommended Posts

Hello

 

I am extending LoaderItem to use with a class called AMFLoader for loading data from AMF services.

package nashi.amf
{
import flash.net.Responder;
import flash.net.NetConnection;
import nashi.amf.AMFService;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.core.LoaderItem;
import flash.events.NetStatusEvent;

public class AMFLoader extends LoaderItem
{
	private var _gateway:String
	private var _parameters:Object;
	private var _serviceName:String
	private var _netconnection:NetConnection

	public function AMFLoader(gateway:String,serviceName:String,parameters:Object=null,vars:Object=null)
	{
		super(vars);
		_type = "AMFLoader"
		_gateway = gateway
		_parameters = parameters==null?{}:parameters
		_serviceName = serviceName
		_netconnection = new NetConnection
	}

	override public function load(flushContent:Boolean=false):void
	{
		super.load(flushContent)
		_netconnection.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusEvent)
		_netconnection.connect(AMFService.GATEWAY_ADDRESS);
		_netconnection.call(_serviceName,new Responder(onResponse,onFail),_parameters)
	}

	private function onNetStatusEvent(e:NetStatusEvent):void
	{
		_netconnection.removeEventListener(NetStatusEvent.NET_STATUS,onNetStatusEvent)
		if(e.info.level=="error"){
			_netconnection.close()
			var loader:AMFLoader = new AMFLoader(
				AMFService.GATEWAY_ADDRESS_DEBUG,
				AMFService.DEBUG_SERVICE_getErrorLog,
				{},
				{onComplete:onDebugComplete,
				onFail:onDebugFail}
			)
		}
	}

	private function onResponse(response:Object):void
	{
		_content = response
		_netconnection.close();
		dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE,this));
	}

	private function onFail(fail:Object):void
	{
		_content = fail.toString()
		_netconnection.close();
		dispatchEvent(new LoaderEvent(LoaderEvent.FAIL,this));
	}

	private function onDebugComplete(evt:LoaderEvent):void
	{
		dispatchEvent(new LoaderEvent(LoaderEvent.ERROR,this,"Could not load data from service <"+_serviceName+"> see log:\n"+_content))
		(evt.target as AMFLoader).netconnection.close()
	}

	private function onDebugFail(evt:LoaderEvent):void
	{
		dispatchEvent(new LoaderEvent(LoaderEvent.ERROR,this,"Could not get error log for service <"+_serviceName+">"))
		(evt.target as AMFLoader).netconnection.close()
	}

	public function get netconnection():NetConnection
	{
		return _netconnection;
	}


}
}

 

The problem is that when I use this loader the completeEvent is not listened by the internal mechanism of LoaderItem.

If I do this:

_loader = new AMFLoader(
			AMFService.GATEWAY_ADDRESS,
			AMFService.FILE_SERVICE_listDir,
			{path:"assets/images/"},
			{onComplete:completeHandler});
		_loader.load()

The completeHandler function does not launch. But if I go:

_loader = new AMFLoader(
			AMFService.GATEWAY_ADDRESS,
			AMFService.FILE_SERVICE_listDir,
			{path:"assets/images/"},
			{onComplete:completeHandler});
		_loader.load()
                       _loader.addEventListener(LoaderEvent.COMPLETE,completeHanlder);

The compleHandler function does work

Any idea?

Link to comment
Share on other sites

You didn't pass the parameters properly to the super class. You passed the vars as the first parameter instead of the 2nd.

 

super(serviceName, vars);

 

Also, it doesn't look like you're cleaning up your stuff internally if/when _dump() is called, so if the loader gets canceled or unloaded or disposed, it won't garbage collect or stop things correctly. I don't have time to analyze all your code but hopefully that helps.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×