Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center /

Exercise 2.6: Separating the model, view, and controller

by Trilemetry

Trilemetry
  • Trilemetry, Inc.

Content

  • Create a new MXML component
  • Move controls to the custom component
  • Pass data to the custom component

Created

2 March 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Builder Flex RIA

Requirements

Prerequisite knowledge

  • Exercise 1.1: Setting up Flash Builder and your project files

User level

Beginning

Required products

  • Flash Builder 4.5 Premium (Download trial)

Sample files

  • ex2_06_starter.zip
  • ex2_06_solution.zip

Model-View-Controller (MVC) is an architectural pattern that separates application UI from data and business logic. In this exercise, you will convert the Employee Portal: Vehicle Request Form (see Figure 1) into an MVC application. The form UI will be moved into a custom component and is the view. The data to populate the form is the model. The controller handles the business logic and is represented by the main application file.

Preview the application.
Figure 1. Preview the application.

In this exercise you will learn how to:

  • Create a new MXML component
  • Move controls to the custom component
  • Pass data to the custom component

Create a new MXML component

In this section you will create a custom component to contain the Employee Portal: Vehicle Request Form user interface, which is the application view.

  1. Download the ex2_06_starter.zip file provided if you haven't already and extract the ex2_06_starter.fxp to your computer.
  2. Open Flash Builder.
  3. Import the ex2_06_starter.fxp file.
  4. Open the ex2_06_starter.mxml file.
  5. Run the application.
  6. Select an employee from the DropDownList control.

    You should see the Office Phone field populates with the employee's phone number.

  7. Return to the ex2_06_starter.mxml file in Flash Builder.
  8. In the Package Explorer view, right-click the src folder and select File > New > Folder.
  9. In the New Folder window, set the Folder name to components and click Finish.
  10. Right-click the components package and select New > MXML Component.
  11. In the New MXML Component dialog box, type VehicleRequestForm for the Name and delete the Width and Height values (see Figure 2).
Create a new MXML Component.
Figure 2. Fill out the New MXML Component dialog.
  1. Click Finish.
  2. In the Package Explorer view, note the VehicleRequestForm.mxml file in the components package (see Figure 3).
View the VehicleRequestForm.mxml file in the Package Explorer view.
Figure 3. View the VehicleRequestForm.mxml file in the Package Explorer view.

Move controls to the custom component

In this section you will move the Form container from the main application and convert it to a custom component.

  1. From within the ex2_06_starter.mxml file, copy the Script block (see Figure 4).
Copy the Script block from the ex2_06_starter.mxml file.
Figure 4. Copy the Script block from the ex2_06_starter.mxml file.
  1. Open the VehicleRequestForm.mxml file.
  2. Paste the copied Script block between the opening Group and Declarations tags.
  3. Within the opening Group tag, add the creationComplete event with a value of init()
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()">
  1. Within the Script block, delete the import statements for the FaultEvent and the ResultEvent packages as well as the employeeService_resultHandler() and employeeService_ faultHandler() functions.
  2. Change the name of the initApp() function to init() and within the function, remove the employeeService.send() method.
  3. Return to the ex2_06_starter.mxml file.
  4. From the Script block, delete the import statement for the CalenderLayoutChangeEvent package and delete the dateChangeHandler() and initApp() functions.
  5. Within the opening Application tag, change the value of the creationComplete event to employeeService.send().
<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="850" creationComplete="employeeService.send()">
  1. From the ex2_06_starter.mxml file cut the Form container block.
  2. In the VehicleRequestForm.mxml file, below the Declarations block, paste the Form container code.
  3. Save the file.
  4. Return to the ex2_06_starter.mxml file and below the Label control code, type <components and use the content assist tool to create an instance of the VehicleRequestForm component (see Figure 5).
Use the content assist to create an instance of the VehicleRequestForm component in the main application file.
Figure 5. Use the content assist to create an instance of the VehicleRequestForm component in the main application file.
  1. Save the file and run the application.
  2. Click the DropDownList control to expand it. You should see there is no data displayed within the control.

Pass data to the custom component

In this section you will use the employees variable you copied to the custom component to populate the component's Form container with data passed from the main application.

  1. Return to the VehicleRequestForm.mxml file in Flash Builder.
  2. Within the Script block, change the employees variable declaration from private to public.
[Bindable] public var employees:ArrayCollection = new ArrayCollection();
  1. Save the file.
  2. In the ex2_06_starter.mxml file, to the components:VehicleRequestForm tag, add the employees property with a value bound to the employees ArrayCollection variable:
<components:VehicleRequestForm employees="{employees}"/>
  1. Save the file and run the application.
  2. Select an employee from the DropDownList control.You should see the application works as it did at the beginning of this exercise.

In this exercise you learned how to create a custom component and pass data to it using custom properties. This architecture is based on the Model-View-Controller design pattern. In the next exercise you will use the application you created in Exercise 1.6 (Creating MXML custom components with ActionScript properties) to create an ActionScript class and use instances of the class to populate employee data.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Exercise 5.2: Defining selector styles
  • Exercise 3.1: Creating an event and dispatching the event object
  • Exercise 4.5: Creating and using item renderers and item editors
  • Exercise 3.2: Extending the event class to pass data in the event object
  • Exercise 2.5: Retrieving and handling data with the RemoteObject object
  • Exercise 2.7: Creating an ActionScript class and instances
  • Exercise 2.8: Creating an ArrayCollection of value objects
  • Exercise 2.9: Use the Data Services wizard to connect to a service
  • Exercise 4.3: Creating item renderers for Spark List components
  • Exercise 4.4: Using the Spark DataGrid control

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement