PDA

View Full Version : Can a Custom Component extend Actionscript Class


Flash Gordon
08-11-2008, 01:51 AM
This is probably a silly question, but I'm new to Flex so it might not hurt to ask. Can a custom component extend an Actionscript class? I have an abstract class that implements some convince methods that I'd like to use. However, it seems I have to aggregate an instance of the component into the another class that extends the Abstract class rather than have the component directly extend the abstract class and apply it's own actionscript methods.

I'm attaching the Flex 3 project, but below is the code in question. Does that make sense what I'm going after? I'd rather the xmlx component be the subclass rather than having to make another actionscript class and aggregate the component into the actionscript class.

What's the standard "Flex-ish" way to do this?


<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="400" height="148"
horizontalAlign="center" verticalAlign="middle"
paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10" xmlns:utils="app.utils.*">

<mx:Script>
<![CDATA[
import com.framework.mvc.IController;
protected var _digiClock:ASDigitalClock;

public function construct(model:IEventDispatcher, controller:IController):void
{
_digiClock = new ASDigitalClock(this, model, controller);
}

]]>
</mx:Script>

<mx:Text id="timeTf" text="Text"/>

</mx:Panel>



package app.views
{
import app.data.ClockInfo;
import app.events.ClockEvent;
import app.utils.TimeFormatter;

import com.framework.mvc.AbstView;
import com.framework.mvc.IController;

import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.IEventDispatcher;

public class ASDigitalClock extends AbstView
{
protected var _component:DigitalClock;
protected var _formatter:TimeFormatter;

public function ASDigitalClock(component:DigitalClock, model:IEventDispatcher, controller:IController=null)
{
super(model, controller);
_component = component;
init();
}

protected function init():void
{
_formatter = new TimeFormatter();
_model.addEventListener(ClockEvent.TICK, onTick, false, 0, true);
}

protected function onTick(e:ClockEvent):void
{
var clockInfo:ClockInfo = e.clockInfo as ClockInfo;
_component.timeTf.text = _formatter.format(clockInfo);
}
}
}



package com.framework.mvc
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

public class AbstView implements IView, IEventDispatcher
{
protected var _dispatcher:EventDispatcher;
protected var _model:IEventDispatcher;
protected var _controller:IController;


/**
* An Abstract View is a convience class to provide some functionality to it's subclasses.
*
* @param model The model which holds the data
* @param controller The controller which interprets the user's input
*/
public function AbstView(model:IEventDispatcher, controller:IController=null)
{
_dispatcher = new EventDispatcher(this);
setModel(model);
if (controller !== null) setController(controller);
}

/**
* Gets the set controller or creates a default one if it hasn't been set yet.
*/
public function getController():IController
{
if (_controller === null)
{
setController( defaultController( getModel() ));
}

return _controller;
}


/**
* Sets the controller and sends the controller a reference of the view
*/
public function setController(v:IController):void
{
_controller = v;
getController().setView(this);
}


/**
* Gets the model reference
*/
public function getModel():IEventDispatcher
{
return _model;
}

/**
* Sets the model
*/
public function setModel(v:IEventDispatcher):void
{
_model = v;
}


/**
* Supplies a default controller that is determined by the subclasses
*/
public function defaultController(model:IEventDispatcher):IControl ler
{
return null;
}


public function update(model:IEventDispatcher, infoObj:Object):void
{
// hook for subclasses
}


/**
* Delegated to EventDispatcher
*/
public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
{
_dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}


/**
* Delegated to EventDispatcher
*/
public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
{
_dispatcher.removeEventListener(type, listener, useCapture);
}


/**
* Delegated to EventDispatcher
*/
public function dispatchEvent(event:Event):Boolean
{
return _dispatcher.dispatchEvent(event);
}


/**
* Delegated to EventDispatcher
*/
public function hasEventListener(type:String):Boolean
{
return _dispatcher.hasEventListener(type);
}


/**
* Delegated to EventDispatcher
*/
public function willTrigger(type:String):Boolean
{
return _dispatcher.willTrigger(type);
}

}
}

dr_zeus
08-12-2008, 06:49 PM
I think you're asking if an MXML component can extend an AS3 class. The answer is absolutely, yes. Here's an example:

<example:MyClass xmlns:example="com.example.controls.*">
</example:MyClass>

Where the super class is com.example.controls.MyClass.

Flash Gordon
08-17-2008, 06:31 AM
Thanks for the answer!

So then MyClass would have to implement IUIComponent then, correct?