简体   繁体   English

Flex / Actionscript:将数据从自定义类传递到主应用程序?

[英]Flex/Actionscript : passing data from custom class to main application?

Ok this probably sounds dumb but Im a complete beginner in Flex programming. 好的,这听起来有些愚蠢,但我是Flex编程的完整入门者。

I have an application with a main .mxml file, and a certain class Foo that I call from the .mxml 我有一个带有主.mxml文件的应用程序,以及一个我从.mxml调用的特定类Foo

In Foo, I make a URLRequest and listen for the Complete event. 在Foo中,我创建一个URLRequest并侦听Complete事件。 Then I found myself with the returned data in a Foo function, but I have no idea how to communicate it to the .mxml part of the applicaton ! 然后,我在Foo函数中发现了返回的数据,但是我不知道如何将其传递给applicaton的.mxml部分! I looked into ArrayCollections but I can't seem to understand how they work and whether it might help. 我研究了ArrayCollections,但似乎无法理解它们如何工作以及是否有帮助。 Isn't there a way to modify, from inside the class, a variable with a global scope ? 没有办法从类内部修改具有全局作用域的变量吗?

This sounds like a small application, but if it's a large application you might want to look at a micro-framework like RobotLegs 这听起来像是一个小型应用程序,但是如果它是大型应用程序,则可能需要查看像RobotLegs这样的微型框架。

If you have your Foo class extend EventDispatcher then it will be able to send events and have the main MXML app listen for said events. 如果您有Foo类扩展EventDispatcher,则它将能够发送事件,并使主MXML应用程序侦听所述事件。

package com.example
{
    import flash.events.EventDispatcher;
    import com.example.events.MyEvent;

    public class Foo extends EventDispatcher 
    {
        public function doAction():void 
        {
            var someData:String = "blah";
            dispatchEvent(new MyEvent(MyEvent.SOMETHING_HAPPENED, someData));
        }
    }
}

A Custom event with a payload (in this case a string) 具有有效负载的自定义事件(在这种情况下为字符串)

package com.example.events 
{
    import flash.events.Event;

    public class MyEvent extends Event 
    {
        static public const SOMETHING_HAPPENED:String = "somethingHappened";

        private var _myData:String;

        public function get myData():String 
        {
            return _myData;
        }

        public function MyEvent(type:String, myData:String, bubbles:Boolean=false, cancelable:Boolean=false) 
        {
            _myData = myData;
            super(type, bubbles, cancelable);
        }

        override public function clone():Event 
        {
            return new MyEvent(type, myData, bubbles, cancelable);
        }
    }
}

Working with your Foo class from the main file: 从主文件处理Foo类:

public function EventDispatcherExample() {
    var foo:Foo = new Foo();
    foo.addEventListener(MyEvent.SOMETHING_HAPPENED, actionHandler);
    foo.doAction();
}

private function actionHandler(e:MyEvent):void {
    trace("my data is: " + e.myData);
}
import mx.core.FlexGlobals;
FlexGlobals.toplevelApplication.varName;

Your Foo class can dispatch an event and have something in you main.mxml listen for that event. 您的Foo类可以调度事件,并且在您的main.mxml中包含一些内容以侦听该事件。 I am sure that I could create an example. 我确信我可以创建一个例子。 I think it is under customer events in Flex documentation. 我认为这是在Flex文档中的客户事件下。 This is assuming I understand the question. 假设我理解这个问题。

As John said, an event is your best choice. 正如约翰所说,活动是您的最佳选择。

If you'd like some example code, I provided some for a similar question here: Data from Popup to Main Application? 如果您想要一些示例代码,我在这里提供了一个类似的问题: 从弹出窗口到主应用程序的数据?

An event might be the best way to do it as it has been stated. 如前所述,事件可能是最好的方法。 Another approach is to dispatch an event like this 另一种方法是调度这样的事件

dispatchEvent(new Event('somethingHappened'));

and also create a get method in your class for the data you need to get. 并在您的类中为需要获取的数据创建一个get方法。

Then all you have to do in your main app is this 那么您在主应用程序中要做的就是

var foo:Foo = new Foo();
foo.addEventListener('somethingHappened', actionHandler);

private function actionHandler(e:Event):void 
{
    trace(foo.memberData);
}

This way might be more suitable if the data should be a class member anyway and if you would like to avoid creating a new event class. 如果数据仍然是类成员,并且您希望避免创建新的事件类,则这种方法可能更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM