Flex 3:同步加载一个XML文件 [英] Flex 3: synchronously loading an xml file

查看:205
本文介绍了Flex 3:同步加载一个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:在flex3中,是否有一种同步加载xml文件的方法?



我知道如何使用load事件异步加载。这可能是有用的,也可能不是。我只是想读取文件,解析它,做我所要做的,继续执行代码。

我有一个使用xml文件存储一些配置参数的组件。我需要在对象初始化时读取文件。但是,对于事件模型,我无法控制何时加载文件,所以我必须编写代码以等待加载代码。这只是荒谬的,还是我?我想要这样的代码:

  var foo:Foo = new Foo(); //这个构造函数应该读取xml并初始化对象。 
foo.doSomething(); //当我调用这个方法时,必须已经处理了xml。

我可以处理事件上的xml文件,并且工作正常,但事件触发 后面的doSomething方法。



我希望我已经解释了我自己。我认为这应该很容易,但是这让我疯狂。我不想写代码来等待事件,除非真的有必要。我觉得这一切应该只是一行代码!

解决方案

无法同步加载,flash是为web而且你永远无法确定一个通话需要多长时间。空气是不同的,因为从文件系统的负载,并没有在那里几乎相同的延迟。

最简单的解决方案是听取负载在Foo内部完成,并从内部调用doSomething(),这样您的外部类将不需要打扰在所有。

如果您确实需要从外部调用foo.doSomething(),则可以使用事件系统。让你的Foo类在完成加载时发送一个事件:

$ $ $ $ $ $ $ dispatchEvent(new Event(Event.COMPLETE,true));

为了解决这个问题,您需要像这样收听:

  foo.addEventListener(Event.COMPLETE,handleFooComplete); 

你的事件处理函数应该是这样的:

  private function handleFooComplete(e:Event):void {
foo.doSomething();





$ b

然而你选择这样做,你需要监听Event.COMPLETE在装载机上。有没有得到解决。


My question is very simple: In flex3, is there a way to load an xml file synchronously?

I know how to load asynchronously, using a load event. This may be useful, or may not. I just want to read the file, parse it, do what I have to do with it, and continue executing code.

I have a component that uses an xml file to store some configuration parameters. I need to read the file when the object is initialized. However, with the event model, I can't control when the file is loaded, so I must write code to "wait" for the code to load. This is just ridiculous, or is it me? I want code like this:

var foo:Foo = new Foo(); //This constructor should read the xml and initialize the object.
foo.doSomething(); //When I call this method the xml must be already handled.

I can handle the xml file on the event, and it works fine, but the event fires after the doSomething method.

I hope I have explained myself. I think this should be really easy, but it's driving me crazy. I don't want to write code to wait for the event unless it's really necessary. I feel all this should be just one line of code!

解决方案

It's not possible to load synchronously, flash is built for the web and you can never be sure how long a call takes. Air is different because that loads from the filesystem, and there are nowhere near the same amounts of delay there.

The cleanest solution would be to listen for the load to complete inside Foo and calling doSomething() from inside, this way your "outer" class won't need to bother at all.

If you do absolutely need to call foo.doSomething() from the outside, you can use the event system. Let your Foo class dispatch an event when it is done loading:

dispatchEvent(new Event(Event.COMPLETE, true));

To catch that you will need to listen for it like so:

foo.addEventListener(Event.COMPLETE, handleFooComplete);

And you event handler function should look like this:

private function handleFooComplete(e:Event):void{
    foo.doSomething();
}

However you choose to do it, you will need to listen for Event.COMPLETE on the loader. There's no getting around that.

这篇关于Flex 3:同步加载一个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆