tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/434-using-robotlegs-app-as-componentRobotlegs: Discussion 2012-01-05T13:08:24Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/116307362011-11-23T18:22:14Z2011-11-23T18:22:14ZUsing Robotlegs App as Component<div><p>Hi Paul,</p>
<p>This discussion is marked as private. Was this your
intention?</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/116307362011-11-23T20:41:34Z2011-11-23T20:41:35ZUsing Robotlegs App as Component<div><p>Opps don't know how it was published as private, thanks.</p>
<p>Paul</p></div>truthrevellertag:robotlegs.tenderapp.com,2009-10-18:Comment/116307362011-11-24T11:40:28Z2011-11-24T11:40:28ZUsing Robotlegs App as Component<div><p>Hi Paul,</p>
<p>In the following code snippets I’ll show you an
oversimplified scenario:<br>
CalendarView (DateChooser)<br>
CalendarMediator<br>
CalendarModel<br>
CalendarEvent<br>
CalendarCommand</p>
<p>The flow:<br>
1. CalenderView.datechooser_changeHandler() ->CalenderEvent->
CalendarMediator-> CalenderEvent->
CalendarCommand->CalendarModel-> CalenderEvent->rest of
the application<br>
2. rest of the application->
CalenderEvent->CalendarMediator-> CalenderView.
initilizeCalendarDate()</p>
<p>The <strong>CalendarMediator</strong> would be the bridge
between your <strong>CalendarView</strong> and the rest of your
application.<br>
If you need to set the date on your DateChooser, you can let
CalendarMediator access a public method in your CalendarView:
initilizeCalendarDate()</p>
<p><strong>CalendarMediator</strong></p>
<pre>
<code>
public class CalendarMediator extends Mediator
{
[Inject]
public var view:CalendarView;
override public function onRegister():void
{
eventMap.mapListener(eventDispatcher, CalendarEvent.INITIALIZE_CALENDAR_DATE, onInitializeCalendarDate);
eventMap.mapListener(view, CalendarEvent.CALENDAR_SET_DATE, dispatch);
view.initilizeCalendarDate('2011/11/11');
}
protected function onInitializeCalendarDate(event:CalendarEvent):void
{
view.initilizeCalendarDate(event.someDate);
}
}</code>
</pre>
<p><strong>CalendarView.mxml</strong></p>
<pre>
<code>
import com.robotlegs.demos.robotlegsincremental.controllers.events.CalendarEvent;
import mx.events.CalendarLayoutChangeEvent;
public function initilizeCalendarDate(dateData:String):void
{
dateChooser.selectedDate=new Date(dateData);
}
protected function datechooser_changeHandler(event:CalendarLayoutChangeEvent):void
{
var date:Date=event.target.selectedDate;
if (date != null)
dispatchEvent(new CalendarEvent(CalendarEvent.CALENDAR_SET_DATE, date));
}</code>
</pre>
<p>If you need to send a chosen date to another component in your
application, you can let CalendarView dispatch an
event(CalendarEvent.CALENDAR_SET_DATE) with the selectedDate as a
payload and CalendarMediator, who has registered an event listener
for it, would re-dispatch it.</p>
<p><strong>CalendarCommand</strong> previously mapped like this:
commandMap.mapEvent(CalendarEvent.CALENDAR_SET_DATE,
CalendarCommand, CalendarEvent, true);</p>
<p>would do this:</p>
<pre>
<code>
public class CalendarCommand extends Command
{
[Inject]
public var calendarEvent:CalendarEvent;
[Inject]
public var calendarModel:CalendarModel;
override public function execute():void
{
calendarModel.someDate=calendarEvent.someDate;
}
}</code>
</pre>
<p><strong>CalendarModel</strong></p>
<pre>
<code>
public class CalendarModel extends Actor
{
protected var _someDate:String;
public function get someDate():String
{
return _someDate;
}
public function set someDate(value:String):void
{
_someDate = value;
dispatch(new CalendarEvent(CalendarEvent.CALENDAR_DATE_CHANGED, someDate));
}
}</code>
</pre>
<p>Other parts in your application would register a listener for
CalendarEvent.CALENDAR_DATE_CHANGED and could use the date.</p>
<p><strong>CalendarEvent</strong></p>
<pre>
<code>
public class CalendarEvent extends Event
{
public static const CALENDAR_SET_DATE:String="calendarSetDate";
public static const CALENDAR_DATE_CHANGED:String="calendarDateChanged";
public static const INITIALIZE_CALENDAR_DATE:String="initializeCalendarDate";
public function CalendarEvent(type:String, someDate:String=null, bubbles:Boolean=false, cancelable:Boolean=false)
{
_someDate=someDate;
super(type, bubbles, cancelable);
}
private var _someDate:String;
override public function clone():Event
{
return new CalendarEvent(type, someDate, bubbles, cancelable);
}
public function get someDate():String
{
return _someDate;
}
}</code>
</pre>
<p>I hope this gives an idea of how to make a DateChooser
reusable.<br>
Let me know what you think:)</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/116307362011-11-24T19:17:33Z2011-11-24T19:17:33ZUsing Robotlegs App as Component<div><p>Wow really great response, your code is really well
structured.</p>
<p>If the Robotlegs Calendar application is inside another
non-Robotlegs application, is there anyway to dispatch events to
the application such as the
"CalendarEvent.INITIALIZE_CALENDAR_DATE" event?</p></div>truthrevellertag:robotlegs.tenderapp.com,2009-10-18:Comment/116307362011-11-25T12:20:59Z2011-11-25T12:20:59ZUsing Robotlegs App as Component<div><p>Hi Paul,</p>
<p>Since you want to communicate between a
<strong>Non-Robotlegs-Application</strong> and a
<strong>Robotlegs-Application loaded as a Module</strong>, the best
way, in my opinion, is to use an <strong>interface</strong>, as
you’d do with any other modules.</p>
<p>But if you want to communicate via events, you can use a
<strong>sharedEventDispatcher</strong> and a
<strong>DynamicEvent</strong>.</p>
<p><strong>NonRobotlegsApplication</strong></p>
<pre>
<code>
Application
import mx.events.ModuleEvent;
import mx.events.DynamicEvent;
protected const MODULE_URL:String="RobotlegsAsModule.swf";
private var sharedEventDispatcher:IEventDispatcher;
protected function loadModule():void
{
sharedEventDispatcher=moduleLoader.loaderInfo.sharedEvents;
sharedEventDispatcher.addEventListener("moduleToMainEvent", onModuleDataChanged, false, 0, true);
moduleLoader.loadModule(MODULE_URL);
}
protected function onModuleDataChanged(event:DynamicEvent):void
{
dataFromModule.text=event.data as String;
}
protected function sendDataToModule():void
{
var dynamicEvent:DynamicEvent=new DynamicEvent("applicationToModuleEevent");
dynamicEvent.data="APPLICATION TO MODULE";
sharedEventDispatcher.dispatchEvent(dynamicEvent);
}</code>
</pre>
<p><strong>RobotlegsAsModule</strong></p>
<pre>
<code>
Module
private var sharedEventDispatcher:IEventDispatcher;
import mx.events.DynamicEvent;
protected function module_creationCompleteHandler(event:FlexEvent):void
{
sharedEventDispatcher=systemManager.loaderInfo.sharedEvents;
sharedEventDispatcher.addEventListener("applicationToModuleEevent", onApplicationDataChanged, false, 0, true);
}
protected function onApplicationDataChanged(event:DynamicEvent):void
{
dataFromApplication.text=event.data as String;
}
protected function sendDataToApplication():void
{
var dynamicEvent:DynamicEvent=new DynamicEvent("moduleToMainEvent");
dynamicEvent.data="ROBOTLEGS MODULE TO APPLICATION";
sharedEventDispatcher.dispatchEvent(dynamicEvent);
}</code>
</pre>
<p>Note that the event listeners have a weak reference!<br>
A strong reference to the sharedEventDispatcher would prevent the
module from being garbage collected.<br>
I hope this helps.<br>
Maybe somebody else will chime in with a better solution, in case
there is one :)</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/116307362011-11-25T18:48:22Z2011-11-25T18:48:22ZUsing Robotlegs App as Component<div><p>I like both your ideas. Thanks Ondina. :)</p></div>truthreveller