Skip to content

Instantly share code, notes, and snippets.

@jmreidy
Created January 23, 2009 18:11
Show Gist options
  • Save jmreidy/51119 to your computer and use it in GitHub Desktop.
Save jmreidy/51119 to your computer and use it in GitHub Desktop.
package com.schedulr.business.events
{
import flash.events.Event;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
public class BaseResponderEvent extends Event
{
public var responder:Responder;
public function BaseResponderEvent( type:String, bubbles:Boolean=true, cancelable:Boolean=false, responder:Responder=null )
{
super( type, bubbles, cancelable );
this.responder = responder;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas verticalPageScrollSize="360" height="360" width="350"
horizontalScrollPolicy="off"
creationComplete="handleCreationComplete( event )"
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:objecthandles="com.roguedevelopment.objecthandles.*"
xmlns:degrafa="http://www.degrafa.com/2007"
xmlns:view="com.schedulr.view.*">
<mx:Script>
<![CDATA[
import com.schedulr.model.SchedulrUtil;
import mx.rpc.events.ResultEvent;
import com.schedulr.business.events.LoadIceTimesEvent;
import mx.binding.utils.ChangeWatcher;
import mx.formatters.DateFormatter;
import mx.core.UIComponent;
import com.schedulr.business.events.IceTimeEvent;
import mx.controls.scrollClasses.ScrollBar;
import mx.events.FlexEvent;
import mx.controls.Alert;
import com.roguedevelopment.objecthandles.ObjectHandleEvent;
import com.schedulr.model.IceTimeVO;
import mx.collections.ArrayCollection;
import mx.rpc.Responder;
public static const PREVIOUS_DAY_VIEW:String = "previousDayView";
public static const TODAY_VIEW:String = "todayView";
public static const NEXT_DAY_VIEW:String = "nextDayView";
private var _times:ArrayCollection;
[Bindable]
public var day:Date;
protected function handleCreationComplete( event:FlexEvent ):void
{
for ( var j:uint = 0; j < 24; j++ )
{
var background:HourBackground = new HourBackground();
background.y = j * 60;
background.hour = j;
background.x = 0;
background.width = this.width - ScrollBar.THICKNESS - 10;
calendarView.addChild( background );
}
ChangeWatcher.watch( this, 'day', loadHours );
}
private function loadHours( ...args ):void
{
dispatchEvent( new LoadIceTimesEvent( LoadIceTimesEvent.LOAD_TIMES_FOR_DATE, day, false, new mx.rpc.Responder( handleTimesLoaded, null ) ) );
}
public function set iceTimes( value:ArrayCollection ):void
{
_times = value;
for each ( var time:IceTimeVO in _times )
{
var timeBox:IceTimeBox = new IceTimeBox();
timeBox.time = time;
timeBox.x = 62;
timeBox.height = ( time.end.hours * 60 + time.end.minutes ) - ( time.start.hours * 60 + time.start.minutes );
timeBox.width = 250;
timeBox.y = ( time.start.hours * 60 + time.start.minutes );
if ( timeBox.height < 0 )
{
timeBox.height = 5;
Alert.show( 'An ice time on ' + SchedulrUtil.formatDateAsString( time.date ) + ' has an end time before the start time!' )
}
calendarView.addChild( timeBox );
}
calendarView.verticalScrollPosition = 710;
}
public function handleTimesLoaded( value:ArrayCollection ):void
{
for each ( var child:* in calendarView.getChildren() )
{
if ( child is IceTimeBox )
{
calendarView.removeChild( child );
}
}
iceTimes = value;
}
]]>
</mx:Script>
<mx:DateFormatter id="DayName" formatString="EEEE" />
<mx:DateFormatter id="DateDisplay" formatString="MM-DD-YY" />
<mx:Label text="{ DayName.format( day ) }" left="5" y="12" styleName="dayLabel" />
<mx:Label text="{ DateDisplay.format( day ) }" right="5" y="22" styleName="dateLabel" />
<mx:Canvas id="calendarView" height="{ this.height - 45 - 10 }" width="{ this.width - 5 }" x="5" y="{ 45 + 5 }" />
</mx:Canvas>
package com.schedulr.model
{
import com.schedulr.business.events.LoadIceTimesEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.utils.Dictionary;
import mx.collections.ArrayCollection;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
public class IceTimeManager
{
private var _dispatcher:IEventDispatcher;
public function IceTimeManager( dispatcher:IEventDispatcher )
{
_dispatcher = dispatcher;
}
[Bindable (event="iceTimesChanged")]
public function get iceTimes():Dictionary
{
return _iceTimes;
}
public function setIceTimes( value:Dictionary ):void
{
_iceTimes = value;
_dispatcher.dispatchEvent( new Event( "iceTimesChanged" ) );
handleResponders();
}
private var _handlers:Array = new Array();
private var _iceTimes:Dictionary = new Dictionary();
public function findTimesForDate( event:LoadIceTimesEvent ):void
{
var times:ArrayCollection;
for ( var key:Object in _iceTimes )
{
if ( SchedulrUtil.compareDates( key as Date, event.selectedDate ) == true )
{
times = _iceTimes[ key ];
}
}
if ( times == null )
{
_handlers.push( { date: event.selectedDate, responder: event.responder } );
_dispatcher.dispatchEvent( new LoadIceTimesEvent( LoadIceTimesEvent.LOAD_DATE_RANGE, event.selectedDate, false ) );
event = null;
}
else
{
notifyCaller( times, event.responder );
}
}
private function handleResponders():void
{
var times:ArrayCollection;
for each ( var listener:Object in _handlers )
{
for ( var key:Object in _iceTimes )
{
if ( SchedulrUtil.compareDates( key as Date, listener.date ) == true )
{
times = _iceTimes[ key ];
notifyCaller( times, listener.responder );
_handlers.splice( _handlers.indexOf( listener ), 1);
continue;
}
}
}
}
protected function notifyCaller( response:*, responder:Responder = null ):void
{
if ( responder == null ) return;
if ( response is FaultEvent && ( responder.fault != null ) )
{
responder.fault( response );
}
else
{
responder.result( response );
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mate:EventMap xmlns:mate="http://mate.asfusion.com/" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import com.schedulr.model.IceTimeManager;
import com.schedulr.business.events.SetSelectedDateEvent;
import com.schedulr.model.Model;
import com.schedulr.model.SchedulrUtil;
import com.schedulr.business.events.LoadIceTimesEvent;
import com.schedulr.business.events.IceTimeEvent;
import com.schedulr.business.delegates.UserDelegate;
import com.schedulr.business.delegates.OrganizationDelegate;
import com.schedulr.business.delegates.IceTimeDelegate;
import mx.events.FlexEvent;
]]>
</mx:Script>
<mate:EventHandlers type="{ FlexEvent.INITIALIZE }" >
<mate:ObjectBuilder generator="{ IceTimeDelegate }" cache="global" constructorArguments="{ [ 'http://localhost:3000/', 'iceTimes', scope.dispatcher ] }" />
<mate:ObjectBuilder generator="{ OrganizationDelegate }" cache="global" constructorArguments="{ [ 'http://localhost:3000/', 'organizations', scope.dispatcher ] }" />
<mate:ObjectBuilder generator="{ UserDelegate }" cache="global" constructorArguments="{ [ 'http://localhost:3000/', 'users', scope.dispatcher ] }" />
<mate:ObjectBuilder generator="{ IceTimeManager }" cache="global" constructorArguments="{ scope.dispatcher }" />
</mate:EventHandlers>
<mate:EventHandlers type="{ FlexEvent.CREATION_COMPLETE }" >
<mate:MethodInvoker generator="{ OrganizationDelegate }" method="indexReq" />
</mate:EventHandlers>
<mate:EventHandlers type="{ SetSelectedDateEvent.DATE_SELECTED }">
<mate:MethodInvoker generator="{ Model }" method="setSelectedDate" arguments="{ event.selectedDate }" />
</mate:EventHandlers>
<mate:EventHandlers type="{ LoadIceTimesEvent.LOAD_TIMES_FOR_DATE }" debug="true">
<mate:MethodInvoker generator="{ IceTimeManager }" method="findTimesForDate" arguments="{ event }" />
</mate:EventHandlers>
<mate:EventHandlers type="{ LoadIceTimesEvent.LOAD_DATE_RANGE }" debug="true">
<mate:InlineInvoker method="{ SchedulrUtil.formatDateAsString }" arguments="{ event.selectedDate }" />
<mate:ObjectBuilder generator="{ Object }" cache="none">
<mate:Properties date="{ lastReturn }" />
</mate:ObjectBuilder>
<mate:MethodInvoker generator="{ IceTimeDelegate }" method="indexReq" arguments="{ lastReturn }" />
</mate:EventHandlers>
<mate:EventHandlers type="{ IceTimeEvent.UPDATE }" >
<mate:MethodInvoker generator="{ IceTimeDelegate }" method="updateReq" arguments="{ [ event.iceTime.id, event.iceTime.objectAsHash ] }" />
</mate:EventHandlers>
</mate:EventMap>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment