到底是什么? Flex 4 中的类?

发布于 2024-12-28 05:55:03 字数 3151 浏览 3 评论 0原文

我现在正在学习 Flex 4.5 的网络部分,我正在复习的课程建议我在每次调用服务器时使用 类,或者使用 < code>组件或使用我自己的对数据库的自定义服务调用。

无论是在课程本身还是在 Adob​​e 文档中,我都找不到关于为什么我应该采用这种方法的真正好的描述。有人可以描述为什么这是一个好主意,并且也许提供一个强烈推荐的案例吗?

请参阅下面的示例,了解我正在使用它的简单案例。我在 标记集内部声明了该类的一个实例,并在 fetchData() 方法内部使用它:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               skinClass="skins.CustomAppSkin">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;

            [Bindable]
            private var booksCollection:ArrayCollection;

            private function formatPrice(data:Object, columns:GridColumn):String {
                return priceFormatter.format(data.price);
            }

            protected function fetchData(event:MouseEvent):void {
                booksResponder.token = books.send();
            }

            protected function processXML(event:ResultEvent):void {
                this.booksCollection = event.result.catalog.book;
            }

            protected function loadHandler(event:FaultEvent):void {
                Alert.show(event.fault.faultString, event.fault.faultCode);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <s:HTTPService id="books" url="data/books.xml"/>
        <s:CurrencyFormatter id="priceFormatter" currencySymbol="$" fractionalDigits="2" trailingZeros="true" useCurrencySymbol="true"/>
        <s:CallResponder id="booksResponder" result="processXML(event)" fault="loadHandler(event)"/>
    </fx:Declarations>

    <s:Panel id="panel" title="Products" horizontalCenter="0">
        <s:DataGrid dataProvider="{booksCollection}" height="400">
            <s:columns>
                <s:ArrayList>
                    <s:GridColumn headerText="Title" width="250" dataField="title"/>
                    <s:GridColumn headerText="Author" dataField="author"/>
                    <s:GridColumn headerText="Genre" width="100" dataField="genre"/>
                    <s:GridColumn headerText="Publish Date" width="100" dataField="publish_date"/>
                    <s:GridColumn headerText="Description" width="400" dataField="description"/>
                    <s:GridColumn headerText="Price (USD)" width="100" dataField="price" labelFunction="formatPrice"/>
                </s:ArrayList>
            </s:columns>
        </s:DataGrid>

        <s:controlBarContent>
            <s:Button id="getData" label="Get Data" click="fetchData(event)"/>
        </s:controlBarContent>
    </s:Panel>
</s:Application>

I am learning the networking portion of Flex 4.5 right now, and the course I am reviewing recommends that I use the <s:CallResponder> class whenever I make a call to a server, either with the <s:HTTPService> component or with my own custom service call to a database.

Both in the course itself and in the Adobe documentation, I cannot find a really good description as to why I should take the approach. Could someone please describe why this is a good idea, and perhaps provide a case where it is highly recommended?

See this example below for a simple case where I am using it. I declare an instance of the class inside of the <fx:Declarations> tagset, and I use it inside of the fetchData() method:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               skinClass="skins.CustomAppSkin">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;

            [Bindable]
            private var booksCollection:ArrayCollection;

            private function formatPrice(data:Object, columns:GridColumn):String {
                return priceFormatter.format(data.price);
            }

            protected function fetchData(event:MouseEvent):void {
                booksResponder.token = books.send();
            }

            protected function processXML(event:ResultEvent):void {
                this.booksCollection = event.result.catalog.book;
            }

            protected function loadHandler(event:FaultEvent):void {
                Alert.show(event.fault.faultString, event.fault.faultCode);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <s:HTTPService id="books" url="data/books.xml"/>
        <s:CurrencyFormatter id="priceFormatter" currencySymbol="$" fractionalDigits="2" trailingZeros="true" useCurrencySymbol="true"/>
        <s:CallResponder id="booksResponder" result="processXML(event)" fault="loadHandler(event)"/>
    </fx:Declarations>

    <s:Panel id="panel" title="Products" horizontalCenter="0">
        <s:DataGrid dataProvider="{booksCollection}" height="400">
            <s:columns>
                <s:ArrayList>
                    <s:GridColumn headerText="Title" width="250" dataField="title"/>
                    <s:GridColumn headerText="Author" dataField="author"/>
                    <s:GridColumn headerText="Genre" width="100" dataField="genre"/>
                    <s:GridColumn headerText="Publish Date" width="100" dataField="publish_date"/>
                    <s:GridColumn headerText="Description" width="400" dataField="description"/>
                    <s:GridColumn headerText="Price (USD)" width="100" dataField="price" labelFunction="formatPrice"/>
                </s:ArrayList>
            </s:columns>
        </s:DataGrid>

        <s:controlBarContent>
            <s:Button id="getData" label="Get Data" click="fetchData(event)"/>
        </s:controlBarContent>
    </s:Panel>
</s:Application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

千仐 2025-01-04 05:55:03

也许一些“历史”背景会有所帮助。 “Responder”这个名称指的是 NetConnection 类,它是 Flash 播放器内置 API 处理通信的方式。当您调用 NetConnection 时(通常用于纯 HTTP 连接,其中消息以 AMF 进行编码,或用于视频下载/流传输,通常是 RTMP 变体连接)。 Responder 与 NetConnection 一起使用,通过 HTTP 使用 AMF 编码进行通信。

有一个内置的 Responder 类,它与 NetConnection 一起使用来存储对两个回调的引用,这两个回调将在通信成功和通信失败时调用。

上述方案与 Flash Player 中存在的其他网络 API 不同。很难思考原因,但从 AS2 开始就是这样。然而,在 AS2 中,有 LoadVars 对象,它也使用错误和成功消息的回调。 XML 类的行为方式类似 - 您还可以创建两个回调函数并将它们分配给默认的 XML 处理程序。响应者并非不可能是 AS3 之前设计的雏形。

当 Adob​​e(出于只有他们自己知道的原因)决定在 NetConnection(在 Flex 中称为 RemoteObject)之上堆积一堆间接层时,他们保留了旧的设计选择。他们还使更新的 API 以更古老的方式运行。因此,例如,HTTPService(基本上是伪装的URLLoader)的行为方式与 RemoteObject 相同。这可能对 MXML 模板有所帮助,但是一旦您意识到 MXML 语言具有极大的局限性,专门为 MXML 布局定制的功能的相对优点就会变成由多个有问题的设计决策组成的一块牌匾。

Amy:据我所知,DCD 功能(Flash Builder 中使用的服务的代码生成)尚未发布到 Apache - 它是 Flash Builder 的一部分。因此,它不太可能改变,除非 Adob​​e 自己改变。

Maybe some "historical" background would help. The name "Responder" refers to the way NetConnection class, which is the Flash player built-in API handles the communication. When you make a call to NetConnection (which is commonly used for either plain HTTP connection, where messages are encoded in AMF, or for video download / streaming, which is usually an RTMP-variant connection). Responder is used with NetConnection to communicate using AMF encoding over HTTP.

There is a built-in Responder class, which is used with NetConnection to store references to two callbacks which will be called in case of successful communication and in the case of communication failure.

The above scheme is different from other networking API that exist in Flash player. It's hard to think about the reason, but it's been like that since AS2. In AS2, however, there was LoadVars object, which also used callbacks for fault and success messages. XML class behaved in a similar way - you would also create two callback functions and assign them to default XML handlers. It's not impossible that responder is a rudiment of the pre-AS3 design.

When Adobe (for the reasons known only to them alone) decided to pile up a bunch of indirection layers on top of NetConnection (known in Flex as RemoteObject) they retained the old design choices. They also made more recent API behave in a more archaic way. Thus, for example, HTTPService, which is basically the URLLoader in disguise, behaves the same way as RemoteObject. This probably helped somewhat in MXML templates, but as soon as you realize that MXML language is extremely limiting, the relative merits of features tailored specifically for MXML layout will rather become a plaque made up of multiple questionable design decisions.

Amy: afaik DCD feature (the code generation for services used in Flash Builder) has not been released to Apache - it's a part of Flash Builder. So, it's unlikely to change, unless Adobe will change that themselves.

街角迷惘 2025-01-04 05:55:03

响应者是一个对象,它具有一个变量,该变量包含对成功结果时调用的函数的引用,并且可以选择包含对发生错误时要调用的函数的引用。如果您没有设置某种类型的响应程序,它就无法调用您的结果和错误处理程序。

A responder is an object that has a variable that contains a reference to a function to call on successful result and can optionally have a reference to a function to call if a fault occurs. If you didn't have a responder of some type set up, it couldn't call your result and fault handlers.

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