ActionScript 3.0 Design Patterns

(Chris Devlin) #1

450 | Chapter 12: Model-View-Controller Pattern


TheMapViewclass (Example 12-17) loads and displays the satellite image, and doesn’t


otherwise interact with the user. As in the combo bo xview, theupdate( )method is


called without any parameters from the constructor (line 16) to load the image for


the default region. Assigning a URL of an image to thesourceparameter in the


UILoadercomponent loads the corresponding image. Unlike the combo box, map


view is a component view that cannot have any children. Therefore, the overridden


update( ) method does not have to call its superclass method.


Building the MVC Triad


The last task is to instantiate the model and controller, and construct the composite


view structure by adding the map view as a child of the combo bo xview. The follow-


ing statements should be executed from the document class of the Flash document.


var model:IModel = new Model( );
var controller:ICompInputHandler= new Controller(model);

// composite view
var view:CompositeView = new CBView(model, controller);
view.x = view.y = 10;
addChild(view);

// adding a child view
var map:ComponentView = new MapView(model);
view.add(map);
map.x = 0
map.y = 40;
addChild(map);

// register to view to recieve notifications from the model
model.addEventListener(Event.CHANGE, view.update);

11 {

12 super(aModel, aController);
13
14 uiLoader = new UILoader( );
15 uiLoader.scaleContent = false;
16 update( );
17 addChild(uiLoader);
18 }
19
20 override public function update(event:Event = null):void
21 {
22 // get data from model and update view
23 uiLoader.source = (model as IModel).getMapURL( );
24 }
25 }
26 }

Example 12-17. MapView.as

Free download pdf