在 Actionscript 3.0 中结合 URLRequest、URLLoader 和完整事件侦听器?

发布于 2024-08-23 22:53:35 字数 690 浏览 7 评论 0原文

在处理数据时,我总是必须编写以下内容:

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

虽然我可以理解这两个对象和事件侦听器分开的有用性,因为它们经常相互协作,但我想知道是否有一种方法可以将它们结合起来全部?我能得到的最接近的是这个,但它有点毫无意义/嵌套:

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

我真正喜欢的是自动组合 URLRequest、URLLoader 和完成的事件侦听器的东西,如下所示:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-08-30 22:53:35

将该代码封装到您自己的类中。它可以像这样简单:

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

像这样使用它:

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

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

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 );
香橙ぽ 2024-08-30 22:53:35

不……这是 adobe 做出的一项架构决策,而且是一个很好的决策。两个两个班级从事截然不同的工作,但都做得很好。事件侦听器允许您多次处理完整的事件,因此在本例中比回调灵活得多。

但是,您可以创建一个包含所有功能并完全按照您希望的方式工作的类!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文