Enabling Multiple Views for One Model


In this example, we have two views that we can use with one model type. We have demonstrated that we can use each view type with the same model, one at a time. Now we'll test that we can use both views simultaneously with the same model instance. To accomplish this goal, we'll modify the main class to create two clock viewsone AnalogClock and one DigitalClock. Each will use the same ClockData object. This change requires just four new lines of code in the main class:

package {    import flash.display.Sprite;    import flash.display.StageAlign;    import flash.display.StageScaleMode;    import com.peachpit.aas3wdp.mvcexample.data.ClockData;    import com.peachpit.aas3wdp.mvcexample.clock.AbstractClockView;    import com.peachpit.aas3wdp.mvcexample.clock.AnalogClock;    import com.peachpit.aas3wdp.mvcexample.clock.DigitalClock;    import com.peachpit.aas3wdp.mvcexample.data.Time;    import flash.utils.Timer;    import flash.events.TimerEvent;    public class ClockTest extends Sprite   {       private var _clockData:ClockData;       public function ClockTest() {                    stage.align = StageAlign.TOP_LEFT;          stage.scaleMode = StageScaleMode.NO_SCALE;                    _clockData = new ClockData();          var clock:AbstractClockView =            new DigitalClock(_clockData);          clock.x = 100;          clock.y = 100;          addChild(clock);                    var clock2:AbstractClockView = new            AnalogClock(_clockData);          clock2.x = 200;           clock2.y = 300;          addChild(clock2);                             var timer:Timer = new Timer(2000, 1);          timer.addEventListener(TimerEvent.TIMER, onTimer);          timer.start();                 }              private function onTimer(event:TimerEvent):void {          _clockData.time = new Time(5, 0, 0);       }        } }


Because both clock views use the same data model, they update at the same time.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net