Adobe flex cairngorm examples


















Presumably you'll always use Cairngorm to access Web services. Cairngorm locates things in common places which is great when you want to understand a new application. I generally create a ProjectName directory below the corresponding Cairngorm directory to handle them.

Some of the examples reuse a single Cairngorm event. However, creating separate events keeps the Value Object parameters typed. It offers a number of classes implementing Model, View, and Controller tiers, and interaction between them.

The Model tier is represented by the class ModelLocator , which stores the application-specific data these are often collections of value objects , a. The View portion contains visual components required by your application, value objects, and Cairngorm-specific event classes used for communication with the Model and Controller tiers. The Controller tier is responsible for invoking appropriate code containing the business logic of your application, which is implemented by using global FrontController and ServiceLocator classes as well as additional Command and Delegate classes.

The List component the View knows about its model and is bound to it directly via its dataProvider property. When a View component fires an event, FrontController finds the Command class that was registered with this event and calls its method execute.

The Command class updates the data in the model typically, a collection of value objects defined in the global ModelLocator. Because each model located inside the ModelLocator is bound to a UI control, its content gets updated automatically. Please note that the code for the six participants mentioned earlier is organized in separate packages folders. The business folder is for delegates and service components. The command folder is for Command classes; control is for events and FrontController ; the ModelLocator is located in the model folder; and the view folder has visual components as shown in Figures through The value objects of the application have been placed in the folder called vo.

Regardless of what framework you are going to use, separating various application components in project subfolders helps make the project more organized. In the example code, CairngormEventDispatcher dispatches the cgEvent :. To eliminate the need to import CairngormEventDispatcher in every view, starting from Cairngorm 2.

On the application startup, the code dispatches LoadEmployeesEvent and, as if by magic, the EmployeeList gets populated from Employees.

How did it happen? This class creates an event with an ID AppController. The next point of interest is the class LoadEmployeesCommand. The method execute must have an argument—the instance of the CairngormEvent object that may or may not encapsulate some application data for example, some value object that is not used in our scenario.

It also implements the interface IResponder , which requires you to add the result and fault methods. By using these callbacks the delegate will return to the command class the result or error information of the execution of the command in question.

This leads to the need for additional coding to convert the data to appropriate types. For example, employee startDate will be stored as a string and will require code to convert it to Date if any date manipulations will be needed. Just move the business logic from the delegate right into the execute method of the command class itself.

Create a common ancestor to all your commands and define the fault method there to avoid repeating the same code in each command class. To load the employees, the Command class creates an instance of the proper delegate passing the reference to itself this is how the delegate knows where to return the data and calls the method loadEmployeesService :.

Have you noticed that the Command class has also reached for the AppModelLocator to be able to update the model? The method loadEmployeesService sends the request to the execution and assigns the responder the instance of LoadEmployees Command , engaging the AsyncToken design pattern described in Chapter 2.

As mentioned previously, each Cairngorm application has a central registry that knows about each and every service that may be used by the application Example For web services, call the method getWebService. ServiceLocator can be used not only as a repository of all services, but also as an authorization mechanism that restricts access to certain application services based on specified credentials. See its methods setCredentials and setRemoteCredentials for details.

The loadEmployeesService class reads Employees. The delegate gets the result and passes it to the result method of the Command class see Example The Command class updates the model. This model locator stores the data and the state of this application—in particular, the variable employeeListDP , which is the place where the list of employees is being stored. Please note that as ActionScript 3 does not support private constructors, the public constructor of this class throws an error if someone tries to improperly instantiate it i.

By using the mx:DataGridColumn element the columns being shown and the i18n column name is used. Also the third column is a custom renderer that creates an edit and delete button for each row. The edit button redirects to the edit page using the navigateToURL method, and delete sends a request to the person service. The Person ActionScript class is very similar to a Java class.

A package is defined, imports, and a class that can contain variables and functions. The class has RemoteClass metadata set on it indicating that if the org.

Person Java class is serialized by either a remoting or a messaging request, Flex will bind the incoming data to the matching ActionScript class. This is the Cairngorm model. This indicates that any changes to values in this class will fire events to anything a value is bound to.

In this case the search DataGrid is bound to the personData ArrayCollection so when the controller updates the data, the DataGrid is automatically updated. The PersonSearchModelLocator is using the standard singleton pattern, but ideally a Dependency Injection DI framework would be used instead to inject the model where it's needed.

This wasn't done to keep this example simpler, and this is actually the suggested way to create a model in Cairngorm. There are multiple DI frameworks are available. Two are Parsley and Spring ActionScript. Spring ActionScript is a SpringSource sponsored project. This is the custom Cairngorm event for retrieving person search data. As was seen in 'search. The PersonController is a front controller and allows mapping of custom events to command implementations for the events.

In execute event:CairngormEvent , which is the implementation of ICommand , a remoting request to the 'personRepository' service is made. A RemoteObject is created passing in the name of the service, and the method matching the Java class on the server is called. It's very simple and straightforward. An event listener is attached to the RemoteObject to listen for a result. An event listener could also be registered to listen for a failure.

Flex has an excellent event model that is easy to leverage for custom events. It retrieves the person id from the event and makes a delete request to the server. Upon success a PersonSearchEvent is fired to display the latest data in the search results. It would be more efficient to just remove the row from the ArrayCollection , but this was done to illustrate how easy it is to perform different tasks in the application once everything is cleanly decoupled using MVC.



0コメント

  • 1000 / 1000