Flex 3:同步加载xml文件

发布于 2024-07-14 18:00:30 字数 578 浏览 7 评论 0原文

我的问题很简单:在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 技术交流群。

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

发布评论

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

评论(5

爱的那么颓废 2024-07-21 18:00:30

不可能同步加载,Flash 是为网络构建的,你永远无法确定调用需要多长时间。 Air 是不同的,因为它是从文件系统加载的,并且那里的延迟量远不及相同。

最干净的解决方案是监听 Foo 内部完成的加载并从内部调用 doSomething() ,这样您的“外部”类根本不需要打扰。

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

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

要捕获该事件,您需要像这样监听它:

foo.addEventListener(Event.COMPLETE, handleFooComplete);

并且您的事件处理函数应该如下所示:

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

无论您选择做什么,您都需要监听 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:

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.

∞觅青森が 2024-07-21 18:00:30

我会回答自己,但我仍然想知道是否有人提出更好的想法。 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?

红墙和绿瓦 2024-07-21 18:00:30

我认为,有一个带有负载的“之后”事件。

因此,您必须将对 new() 的调用和对 do() 的调用拆分为两个不同的方法,因此 new() 在初始化中调用,而 do() 在 load() 之后调用

伪语法:

beforeInitialisation()
  disableDoSomething()
  new()...
  loader.addEvent(AFTERLOAD, afterLoad)


afterLoad()
  enableDoSomething()


someMethod()
  doSomething()

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:

beforeInitialisation()
  disableDoSomething()
  new()...
  loader.addEvent(AFTERLOAD, afterLoad)


afterLoad()
  enableDoSomething()


someMethod()
  doSomething()
情绪 2024-07-21 18:00:30

老问题,但我敢打赌很多人仍然来这里,所以,这是我的解决方案。

创建一个在处理 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().

┾廆蒐ゝ 2024-07-21 18:00:30

从 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.

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