Adobe
Products

Top destinations

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • SiteCatalyst
  • Students
  • Elements family

Adobe Creative Cloud

  • What is Adobe Creative Cloud?
  • Design
  • Web
  • Photography
  • Video
  • Students
  • Teams
  • Enterprise
  • Educational institutions

Design and photography

  • Photoshop
  • Illustrator
  • InDesign
  • Adobe Muse
  • Lightroom

Video

  • Adobe Premiere
  • After Effects

Web development and HTML5

  • Edge Tools & Services [opens in a new window]
  • Dreamweaver
  • Gaming [opens in a new window]

Adobe Marketing Cloud

  • What is Adobe Marketing Cloud?
  • Digital analytics
  • Social marketing
  • Web experience management
  • Testing and targeting
  • Media optimization

Analytics

  • SiteCatalyst
  • Adobe Discover
  • Insight

Social

  • Adobe Social

Experience Manager

  • CQ
  • Scene7

Target

  • Test&Target
  • Recommendations
  • Search&Promote

Media Optimizer

  • AdLens
  • AudienceManager
  • AudienceResearch

Document services

  • Acrobat
  • EchoSign [opens in a new window]
  • FormsCentral [opens in a new window]
  • SendNow [opens in a new window]
  • Acrobat.com [opens in a new window]

Publishing

  • Digital Publishing Suite

  • See all products
Business solutions

By business need

  • Digital analytics
  • Digital publishing
  • Document management
  • Media optimization
  • Social marketing
  • Testing and targeting
  • Video editing and serving
  • Web development [opens in a new window]
  • Web experience management
  • See all business needs

By industry

  • Broadcast
  • Education
  • Financial services
  • Government
  • Publishing
  • Retail
  • See all industries
Support & Learning

I need help

  • Products
  • Adobe Creative Cloud
  • Adobe Marketing Cloud
  • Forums [opens in a new window]

I want to learn

  • Training and tutorials
  • Certification [opens in a new window]
  • Adobe Developer Connection
  • Adobe Design Center
  • Adobe TV [opens in a new window]
  • Adobe Marketing Center
  • Adobe Labs [opens in a new window]
Download
  • Product trials
  • Adobe Flash Player
  • Adobe Reader
  • Adobe AIR
  • See all downloads
Company
  • Careers at Adobe
  • Investor Relations
  • Newsroom
  • Privacy
  • Corporate Social Responsibility
  • Customer Showcase
  • Contact us
  • More company info
Buy
  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers
  • Adobe Marketing Cloud sales [opens in a new window]
Search
 
Info Sign in
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Welcome,
My Adobe
My orders
My information
My preferences
My products and services
Sign out
My cart
Privacy My Adobe
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out Privacy 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
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
Review and Checkout
Adobe Developer Connection / Flex Developer Center / Flex in a Week /

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 BuilderFlexRIA
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

Requirements

Prerequisite knowledge

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

User level

Beginning

Required products

  • Flash Builder 4.7 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.10: Creating a production build
  • Exercise 5.8: Animating Button components
  • Exercise 3.4: Passing data to the server with the WebService class
  • Exercise 3.7: Using two-way binding
  • Exercise 2.8: Creating an ArrayCollection of value objects
  • Exercise 3.4: Passing data to the server with the RemoteObject class
  • Exercise 3.6: Validating form data
  • Exercise 4.4: Using the Spark DataGrid control
  • Exercise 4.6: Navigating using navigator containers
  • Exercise 5.1: Using text controls

Products

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • Digital Publishing Suite
  • Elements family
  • SiteCatalyst
  • For education

Download

  • Product trials
  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR

Support & Learning

  • Product help
  • Forums

Buy

  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers

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 © 2013 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

Ad Choices

Reviewed by TRUSTe: site privacy statement