AS3:如何组织代码,以防调用远程服务

发布于 2024-08-09 17:27:45 字数 1265 浏览 10 评论 0原文

在我的 Flex 应用程序中,我经常使用服务。我需要他们更新应用程序中的数据,所以经常打电话给他们。目前我是通过以下方式实现的:

1)有一个服务提供者(AMFLoader类) 2) 响应处理程序(Responder 类)

代码如下所示:

public function AMFLoader(url:String):void
{
     gateway = new NetConnection();
     gateway.connect(url);
}

public function callAMFLoader(serviceName:String, param:String,  resultHandler:AMFResultHandler):void
{
     gateway.addEventListener(IOErrorEvent.IO_ERROR, resultHandler.onIENetError);
     gateway.addEventListener(NetStatusEvent.NET_STATUS, resultHandler.onNetStatus);
     responder = new Responder(resultHandler.onSuccess, resultHandler.onFalse);
     gateway.call(serviceName,responder, param);

}

和结果处理程序类:

public class AMFResultHandler
{
    public function AMFResultHandler()
    {
    }


    public function onSuccess(result:*):void
    {
        trace("Result from basic AMF Result handler on success: " + result);
    }

为了调用服务,我扩展了基本结果处理程序类,并创建自定义函数来处理结果。通常在最新阶段,我将从服务接收的数据绑定到我在主应用程序中定义的全局变量,然后在其他类中使用它。

目前我注意到使用全局变量是一件非常烦人的事情,不确定这是否是一种好的编程风格。 例如,当我尝试重构我的代码时,我注意到有时很难理解谁以及何时(以及为什么)在那里填充了数据。

也许您可以建议一种存储服务调用数据的方法。我无法理解的主要问题是我们不能只创建一个方法(例如 getData),然后在应用程序中的某个位置调用它,因为调用服务和存储数据是不同的事情(因为仅在某些事件上接收数据) ....

提前致谢。

In my flex application I use services a lot. I need them to update my data in the application, so call them quite often. Currently I implemented it in the following way:

1) There is a service provider (AMFLoader class)
2) And response handler (Responder class)

The code looks like this:

public function AMFLoader(url:String):void
{
     gateway = new NetConnection();
     gateway.connect(url);
}

public function callAMFLoader(serviceName:String, param:String,  resultHandler:AMFResultHandler):void
{
     gateway.addEventListener(IOErrorEvent.IO_ERROR, resultHandler.onIENetError);
     gateway.addEventListener(NetStatusEvent.NET_STATUS, resultHandler.onNetStatus);
     responder = new Responder(resultHandler.onSuccess, resultHandler.onFalse);
     gateway.call(serviceName,responder, param);

}

and Result handler class:

public class AMFResultHandler
{
    public function AMFResultHandler()
    {
    }


    public function onSuccess(result:*):void
    {
        trace("Result from basic AMF Result handler on success: " + result);
    }

To make a call to a service I extend basic Result handler class, and make custom functions to process results. Usually on the latest stages, I'm binding data received from the service to the global variables which I defined in the main application, and then use it in other classes.

Currently I noticed that this is quite an annoying thing to use global variables, not sure if it's a good programming style to do it.
For example when I tried to refactor my code, I noticed that it's sometimes hard to understand who and when (and why), populated data there.

Maybe you can suggest a way of storing the data from services calls. The main thing I am failing to understand is that we can't just create a method, e.g. getData, and call it somewhere in the application, because calling service and storing the data are different things (as data is received only on some event)....

Thanks in advance.

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

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

发布评论

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

评论(3

烟凡古楼 2024-08-16 17:27:45

您所问的是 MVC 框架的主要目的之一。特别是您谈论的模型或数据接口。如果您还不想使用完整的框架,您应该做的是创建 2 个类。

1) DataObject

2) DataModel

,其中“数据”被替换为直观的内容,例如用户或产品。在 DataObject 中,您只保留数据的属性,同样,它是唯一假设数据正确的地方。但 DataObject 只能通过 DataModel 直接访问。在数据模型中,您保留 DataObject 的所有 getter 和 setter 以及填充 DataObject 所需的任何远程服务调用。

这只是 MVC 风格应用程序开发的开始,如果您确实想转向更灵活、更易于管理的代码,您应该考虑使用完整的框架。我推荐的两个是:

1)Swiz(http://swizframework.org/):一个超级简单的事件驱动真正发挥 Flex 力量的框架。

2)PureMVC( http://puremvc.org/ ):一种易于理解的MVC代码结构,强调代码解耦以及严格的MVC架构。

我说从这两者之一开始,你就会顺利成为一名更好的 Flex 开发人员。


一些很棒的教程:

swiz:http://vimeo.com/7166397

pureMVC:http://active.tutsplus.com/tutorials/workflow/understanding-the -puremvc-开源框架/

What you're asking about is one of the main purposes of an MVC framework. Specifically your talking about the Model or data interface. What you should do, if you don't want to go with a full blown framework just yet, is create 2 classes.

1) DataObject

2) DataModel

where "Data" is replaced with something intuitive like User or Products. In the DataObject you keep only properties of the data and similarly it is the only place where data is assumed to be correct. The DataObject though is only accessed directly through the DataModel. Inside the data model you keep all the getters and setters of the DataObject as well as any remote service calls you need to populate the DataObject.

This is just the beginning of MVC style application development and if you truly want to move on to more flexible, manageable code you should consider going with a full blown framework. Two that i recomend are:

1) Swiz ( http://swizframework.org/ ) : a super simple event driven framework that really leverages the power of flex.

2) PureMVC ( http://puremvc.org/ ) : an easy to understand MVC code structure that emphasizes code decoupling and strict MVC architecture.

I say start with one of those two and you'll be well on your way to becoming a better flex developer.


a couple great tutorials:

swiz: http://vimeo.com/7166397

pureMVC: http://active.tutsplus.com/tutorials/workflow/understanding-the-puremvc-open-source-framework/

情释 2024-08-16 17:27:45

有几个 Flex 框架可以帮助您:

这些框架中的每一个都将帮助您更好地组织模型和服务。逐一查看并选择您觉得舒服的一个。

There are several Flex frameworks that can help you:

Each of these frameworks will help you better organize your models and services. Take a look at each of them and choose one that you're comfortable with.

盗琴音 2024-08-16 17:27:45

我个人也很喜欢Mate。我真的正在挖掘他们的框架,并且正在针对我目前正在进行的几个项目对其进行改造。

I personally like Mate as well. I'm really digging their framework and am retro-fitting it for a few projects I'm currently working on right now.

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