ViewDisptcher
/**
* Copyright katapad ( http://wonderfl.net/user/katapad )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2YbZ
*/
package
{
import com.bit101.components.PushButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
public class TestDispatch extends Sprite
{
private var _model:Model;
public function TestDispatch()
{
_init();
}
private function _init():void
{
_model = new Model();
addChild(_model);
_model.x = 200;
_model.y = 50;
addChild(new View());
ViewDispatcher.getInstance().addEventListener('unko1', _unko1Handler);
ViewDispatcher.getInstance().addEventListener('unko2', _unko2Handler);
}
private function _unko1Handler(event:Event):void
{
_model.update(event.type + 'chan');
}
private function _unko2Handler(event:Event):void
{
_model.update(event.type + 'man');
}
}
}
import com.bit101.components.PushButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
class View extends Sprite
{
function View():void
{
new PushButton(this, 50, 50, 'btn1', _viewclick1)
new PushButton(this, 50, 80, 'btn2', _viewclick2)
}
private function _viewclick1(e:Event):void
{
ViewDispatcher.getInstance().dispatchEvent(new Event('unko1'));
}
private function _viewclick2(e:Event):void
{
ViewDispatcher.getInstance().dispatchEvent(new Event('unko2'));
}
}
class Model extends Sprite
{
private var _tf:TextField;
function Model():void
{
_tf = new TextField();
_tf.autoSize = 'left';
addChild(_tf);
_tf.defaultTextFormat = new TextFormat('_sans', 10);
_tf.text = 'this textfield is Model';
}
public function update(text:String):void
{
_tf.text = text;
}
}
import flash.events.EventDispatcher;
class ViewDispatcher extends EventDispatcher
{
function ViewDispatcher():void
{
}
private static var _instance:ViewDispatcher;
/**
* getInstance
*/
public static function getInstance():ViewDispatcher
{
if (_instance == null)
_instance = new ViewDispatcher();
return _instance;
}
}