`
oywl2008
  • 浏览: 1004012 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Flex 开发入门

 
阅读更多

http://www.ibm.com/developerworks/cn/web/wa-lo-flexdev/

 

Flex 编程基础

面向对象的编程

在上面 Hello World 的例子中我们可以看出,就像在 HTML 中嵌入 JavaScript 那样,我们可以在 mxml 里面嵌入 Action Script 代码来实现业务逻辑。没错!如果您把 Flex 中 mxml 和 Action Script 的关系理解为 Html 和 JavaScript 的关系,您会忽然发现您对 Flex 变的如此熟悉!

Action Script 语言是面向对象的脚本语言,它连编写方式都和 JavaScript 非常的相似。除了可以嵌套在 mxml 里面之外,它还可以像 JavaScript 写在单独的 .js 文件里面那样写在单独的 .as 文件里面,然后在 mxml 里面引入它。

 

 

Parsley

Framework: Parsley

Website: http://www.spicefactory.org/

Developer: Jens Halm

Version: 2.0.0

License: Open source

Configuration: XML/MXML/ActionScript

Parsley is another mature IOC framework that was originally inspired by Spring. It has recently undergone a major rewrite. The new version employs native Flex features like binding and metadata to give you greater options for configuring your project.

Core concepts

Central to Parsley is the idea of a context. This comes from Spring and refers to the dependency injection configuration for the application.

Configuration in Parsley is now available in a variety of options, including XML and MXML. You can use native MXML markup or custom MXML tags from the Parsley library. To enable the injection mechanism, Parsley uses metadata tags, similar to the Swiz framework.

Parsley comes with messaging patterns too. With very little intrusive code, you can enable your objects as event sources or event handlers. I used this capability in this example instead of using the Controller pattern.

Basic Parsley configuration

There are three basic steps to Parsley configuration:

Create a Config.mxml file

Initialize a Context in the root of the application

Add the Inject metadata to the dependencies in your views.

Although there are other options as to how you prepare your configuration file, for this example I'm using an MXML file with native markup and Parsley tags. This approach has the benefit of including classes at compile time at the expense of not being able to update the configuration of an already compiled application.

Object factory and object configuration

In the Config.mxml you will see all the application objects, from domain models to delegates. They are declared in two ways:

In standard MXML

Using Parsley's object definition tags

I will go into more detail of these two types in a later section.

Setting up the controller (and LoginHandler)

Instead of using my hand-rolled controller, I have used Parsley's messaging system, which is designed to have a minimal impact on the objects that you write. It makes use of metadata to do this. An object that is visible to the Context and has this metadata will allow Parsley to wire up the event source to the event handler.

In the example application, LoginPM is the event source, and LoginAction (which I've renamed from LoginHandler) is the event handler.

Here is an excerpt from LoginPM:

[Event( name="LOGIN", type="com.adobe.login.control.event.LoginEvent")] [ManagedEvents("LOGIN")] public class LoginPM extends EventDispatcher { ... public function login() : void { var event : LoginEvent = new LoginEvent( username, password ); dispatchEvent( event ); } }

The three items that enable this as an event source are the Eventmetadata tag, the ManagedEventsmetadata tag, and EventDispatcher#dispatchEvent. Of these three, only ManagedEventsis a Parsley-specific addition. The Event metadata is just good practice, and dispatchEventis required to do the work. Parsleywill use ManagedEventsto determine which events it needs to handle and delegate to event handlers.

 

Here is an excerpt from LoginAction, which has been configured as an event handler:

public class LoginAction implements IResponder { [MessageHandler] public function execute( event : LoginEvent ) : void { ... } }

Because I added MessageHandler metadata to this function, Parsley will add this object/function as a listener to any event of type LoginEvent.

To make these objects visible to Parsley, I can declare the object within the configuration file that I pass into FlexContextBuilder or I can use the Configure object in my views.

Injecting the presentation models

As with all the examples, I’ve made the presentation models nonhierarchical. See the explanation in the Spring ActionScript section for more information.

Parsley supports both setter injection and constructor injection. As I noted with the Spring ActionScript example, I prefer constructor injection because it exposes the dependencies that the object needs to function. Below is the configuration for DashboardPM:

<spicefactory:Object type="{ DashboardPM }"/>

If your object requires constructor arguments, you’ll want to declare it using an Object tag because such arguments are not supported in native MXML.

To complete the constructor, you add some metadata to your class:

[InjectConstructor] public class DashboardPM { public var user : User; public function DashboardPM( user : User ) { this.user = user; } ... }

The metadata tag InjectConstructorinstructs Parsleyto inject a declared object of type Userinto the constructor of DashboardPM.

For setter injection you just need to add the Injectmetadata tag to your classes. For example, I declare SummaryPM in standard MXML inside Config:

<dashboard:SummaryPM/>

In the class file, I then have:

public class SummaryPM { [Inject] public var friends : Friends; ... }

The Inject tag indicates that an instance of type Friends needs to be injected into this instance.

Parsley summary

Inspired by some of the innovations that the other frameworks have pioneered, the new version of Parsley is a complete IOC framework. It also supports modular development to support unloading of contexts. This is an important feature for a growing number of Flex applications that make use of modules.

http://www.adobe.com/devnet/flex/flex_java.html

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics