views:

1252

answers:

3

when handling data, i always have to write the following:

var dataSourceRequest:URLRequest = new URLRequest("/path/file.xml");
var dataSourceLoader:URLLoader = new URLLoader(dataSourceRequest);
dataSourceLoader.addEventListener(Event.COMPLETE, handleDataSource);

while i can understand the usefulness of these 2 objects and event listener being separate, since they often work with each other i'd like to know if there is a method that will combine them all? the closest i can get is this, but it's a bit pointless/nesting:

var dataSourceLoader:URLLoader = new URLLoader(new URLRequest("/path/file.xml"));
dataSourceLoader.addEventListener(Event.COMPLETE, handleDataSource);

what i'd really love would be something that automatically combines the URLRequest, URLLoader and completed event listener like this:

var dataSource:Whatever = new Whatever("/path/file.xml", handleDataSource);
+2  A: 

No... it's an architectural decision that adobe made and a good one at that. Two two classes do very different jobs and do those jobs well. The event listener allows you to handle the complete event multiple times and so is a lot more flexible than a callback in this instance.

You could however create a class that wraps all that functionality and works in exactly the way you want it to!

James Hay
mostly i can understand why the event listener should remain separate, but i'm having a difficult time trying to imagine a URLRequest without a URLLoader (vice/versa), so i'm a little curious why there is no built-in option that will combine at least those two. like writing "new", which i assume is the same here as it is in other languages, that does both allocation of memory and then initiates the object.
TheDarkInI1978
URLRequest can capture more than just a URL which is why it's is own object. And yes URLLoader does require a URLRequest. But then so do classes such as Loader and Sound. So you can see URLRequest is used in multiple places which is another good place. Making them seperate gives us developers much more flexibility and is there so we can create object like the custom loader you mentioned.
James Hay
+1  A: 

Encapsulate that code into your own class. It could be as simple as this:

package
{
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class EncapsulatedURLLoader
    {
        protected var _callback:Function;

        public function EncapsulatedURLLoader( dataUrl:String, callback:Function )
        {
            _callback = callback;
            var l:URLLoader = new URLLoader();
            l.addEventListener( Event.COMPLETE, onComplete );
            l.load( new URLRequest( dataUrl ) );
        }

        private function onComplete( event:Event ):void 
        {
            event.target.removeEventListener( Event.COMPLETE, onComplete );
            _callback.call( null, event.target.data );
        }
    }
}

Use it like so:

function onLoaded( data:* ):void
{
    trace( data );
}

var l:EncapsulatedURLLoader = new EncapsulatedURLLoader( "xml/data.xml", onLoaded );
Matt W
A: 

This is genius, saved my day

kristian Andersen