开发者

Need help understand MVC implementation within Actionscript 3 AS3, please

I am learning MVC implementation with ActionScript 3.0. So far, I have done alright but have couple of questions that can make this learning process very pleasant for me. I would highly appreciate your help and wisdom.

Let me explain how I am implementing a simple MVC implementation:

My Flash Movie is called FusionMVC. I have all the MVC files within the same package like this:

DataModel DataControl DataView Application Facade

Here are my question:

As I understand it correctly, whenever I need to place a display object on the main stage, I declare or implement that display object in DataView class, am I right?

I have a symbol/display object called "Box" in the main library. I add this symbol to the stage by instantiating it within DataView class which I am able to see at runtime. Now if I need to add an EventListener called "ClickHandler" to this object:

Where do I declare this "ClickHandler" event, please? I am currently declaring it in the DataModel Class.

What I am confused about is to where to declare the EventHandler Methods. DataModel, or DataControl?

Thank you for your help. Here is the entire code:

//DATAMODEL

package
{
    import flash.events.*;
    import flash.errors.*;

    public class DataModel extends EventDispatcher
    {
        public static const UPDATE:String = "modelUpdaed";
        private var _txt:String;

        public function DataModel()
        {

        }

        public function get Text():String
        {
            return _txt;
        }

        public function set Text(p:String开发者_高级运维):void
        {
            _txt = p;
            notifyObserver();
            trace("MODEL HAS BEEN CHANGED");
        }

        public function notifyObserver():void
        {
            dispatchEvent(new Event(DataModel.UPDATE));
        }

        public function sayHello(e:Event):void
        {
            trace("HEY HELLO");
        }

    }
}

//DATACONTROL

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.errors.*;

    public class DataControl
    {
        private var _model:DataModel;

        public function DataControl(m:DataModel)
        {
            _model = m;
        }


    }
}

//DATAVIEW

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.errors.*;

    public class DataView extends Sprite
    {
        private var _model:DataModel;
        private var _control:DataControl;
        public var b:Box;

        public function DataView(m:DataModel, c:DataControl)
        {
            _model = m;
            _control = c;

            addBox();
        }

        public function addBox():void
        {
            b = new Box();
            b.x = b.y = 150;
            //b.addEventListener(MouseEvent.CLICK, vHandler);
            addChild(b);
        }


    }
}

//APPLICATION FACADE

package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.errors.*;

    public class Main extends Sprite
    {
        private var _model:DataModel;
        private var _control:DataControl;
        private var _view:DataView;

        public function Main()
        {
            _model = new DataModel();
            _control = new DataControl(_model);
            _view = new DataView(_model, _control);

            addChild(_view);
        }
    }
}


You are correct - display objects should handle events from their own children:

public function addBox():void
    {
        b = new Box();
        b.x = b.y = 150;
        b.addEventListener(MouseEvent.CLICK, boxClickHandler);
        addChild(b);
    }

Handle the event in the view:

private function boxClickHandler(event:MouseEvent)
{
    _control.sayHello();
}

And then you just need to add to your controller the method to change the model:

public class DataControl
{
....
public function sayHello():void
{
    _model.sayHello();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜