Flex 3:同步加载xml文件
我的问题很简单:在flex3中,有没有办法同步加载xml文件?
我知道如何使用加载事件异步加载。 这可能有用,也可能没有用。 我只想读取文件,解析它,做我必须做的事情,然后继续执行代码。
我有一个使用 xml 文件来存储一些配置参数的组件。 我需要在初始化对象时读取该文件。 但是,使用事件模型,我无法控制文件何时加载,因此我必须编写代码来“等待”代码加载。 这太可笑了,还是我? 我想要这样的代码:
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.
我可以处理事件上的 xml 文件,并且它工作正常,但是事件在 doSomething 方法之后触发。
我希望我已经解释清楚了。 我认为这应该很容易,但这让我发疯。 我不想编写代码来等待事件,除非确实有必要。 我觉得这一切应该只是一行代码!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不可能同步加载,Flash 是为网络构建的,你永远无法确定调用需要多长时间。 Air 是不同的,因为它是从文件系统加载的,并且那里的延迟量远不及相同。
最干净的解决方案是监听 Foo 内部完成的加载并从内部调用 doSomething() ,这样您的“外部”类根本不需要打扰。
如果您确实需要从外部调用 foo.doSomething(),则可以使用事件系统。 让您的 Foo 类在加载完成后分派一个事件:
要捕获该事件,您需要像这样监听它:
并且您的事件处理函数应该如下所示:
无论您选择做什么,您都需要监听 Event .在装载机上完成。 这是无法回避的。
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:
To catch that you will need to listen for it like so:
And you event handler function should look like this:
However you choose to do it, you will need to listen for Event.COMPLETE on the loader. There's no getting around that.
我会回答自己,但我仍然想知道是否有人提出更好的想法。 dirkgently 和 Peter Miehle 的回答都很有帮助,但并不能解决我的实际问题。
看来同步加载是不可能的。 加载可能会失败或花费太长时间,我们不能因此而冻结应用程序。 这是合理的,但我仍然认为它会使代码变得比应有的更复杂。
我要做的是在创建对象之前加载 xml 文件,并将其作为参数传递给构造函数。 这样我就可以确保该对象在需要时加载了 xml 文件。 然而,这也不是一个理想的解决方案,因为现在我必须让另一个类负责 Foo 的“私有事物”。
还有更好的想法吗?
I will answer myself, however I'd still like to know if someone comes up with better ideas. Answers from dirkgently and Peter Miehle are both helpful but does not solve my actual problem.
It seems synchronous loading is just not possible. The loading could fail or take too long, and we can't afford to freeze the application just because of that. That's reasonable, however I still feel it can make code more complicated than it should.
What I'm going to do is loading the xml file before creating the object, and passing it as a parameter to the constructor. That way I'll make sure the object has the xml file loaded when it's needed. This however is also not an ideal solution, because now I have to make another class responsible for the "private things" of Foo.
Any better ideas?
我认为,有一个带有负载的“之后”事件。
因此,您必须将对 new() 的调用和对 do() 的调用拆分为两个不同的方法,因此 new() 在初始化中调用,而 do() 在 load() 之后调用
伪语法:
i think, there is an "after" event with load.
so you have to split the call to the new() and the call to the do() in two distinct methods, so the new() is called in initalisation and do() is calld after loading()
pseudosyntax:
老问题,但我敢打赌很多人仍然来这里,所以,这是我的解决方案。
创建一个在处理 resultEvent 的函数内部分派的自定义事件
让您想要做某事的对象(我认为在本例中是 foo)来侦听此自定义事件,然后该事件将调用 doSomething()。
Old question but I bet a lot of people still come here so, here's my solution.
Create a custom event that you dispatch inside of the function that handles the resultEvent
Have the object you want to do something, i think in this case it was foo, to listen for this custom event which then would call doSomething().
从 XML 加载事件的事件处理程序内部调用 foo.doSomething(); (例如,
EVENT.Complete
或类似的内容,具体取决于您加载文件的方式)。 这是推荐的方法。Call
foo.doSomething();
from inside an event handler for the XML loading event (e.g.EVENT.Complete
or somesuch depending on how exactly you are loading the file). That is the recommended way to go.