A getter for a getter is not an unheard of thing in the role of actors such as Mediator, Proxy and Facade (which are all very similar in that they all act sort of like a broker or go-between).
Here is where we want to have the component expose an API. We dont want the mediator OR the command to reach into the component to get its slider's current position. We want our component that has a slider to expose a public variable that gets its value via binding from that slider's value.
from
MyComponent:
<mx:Script>
<![CDATA[
public static const COOLNESS_CHANGED:String = 'coolnessChanged';
public var coolness:Number;
public var item:ItemVO;
]]>
</mx:Script>
<mx:Binding destination="coolness" source="coolnessSlider.value"/>
<mx:VSlider id="coolnessSlider" value="{coolness}"
minimum="0" maximum="10"
change="dispatchEvent(new Event(COOLNESS_CHANGED))"/>
<!-- Display the item -->
<view:MyItemDisplayComponent id="itemDisplay" item="{item}"/>
Parts of the
MyComponentMediator might look like the following (note there's no need for the Mediator to know about the slider, either, leaving us free to refactor the slider to something else if we want, like a combo box):
private function get myComponent():MyComponent
{
return viewComponent as MyCompoent;
}
override public function onRegister():void
{
myComponent.addEventListener( MyComponent.COOLNESS_CHANGED, onCoolnessChanged);
}
override public function handleNotification( note:INotification ):void
{
switch (note.getName())
{
case ItemProxy.ITEM_RETRIEVED:
case ItemProxy.ITEM_UPDATED:
myComponent.item:ItemVO = note.getBody() as ItemVO;
myComponent.coolness=itemVO.coolness;
break;
}
}
private function onCoolnessChanged(event:Event):void
{
sendNotification(ApplicationFacade.APPLY_COOLNESS, [myComponent.item, myComponent.coolness]);
}
Then inside ApplyCoolnessCommand (notice there's no need to ask the mediator or to know the slider):
override public function execute( note:INotification ):void
{
var args:Array = note.getBody();
var item:Number = args[0];
var coolness:Number = args[1];
if (coolness == item.coolness) return; // debounce
var itemProxy:ItemProxy = facade.retriveProxy(ItemProxy.NAME) as ItemProxy;
itemProxy.updateCoolness(args); // this will result in a ItemProxy.ITEM_UPDATED note being sent...
// do other stuff that justified doing this in a command instead of back in the Mediator
}
Hope this helps,
-=Cliff>