在 Actionscript 3.0 中结合 URLRequest、URLLoader 和完整事件侦听器?
在处理数据时,我总是必须编写以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将该代码封装到您自己的类中。它可以像这样简单:
像这样使用它:
Encapsulate that code into your own class. It could be as simple as this:
Use it like so:
不……这是 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!