Skip to content

Instantly share code, notes, and snippets.

@gavJackson
Created October 23, 2012 15:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavJackson/3939322 to your computer and use it in GitHub Desktop.
Save gavJackson/3939322 to your computer and use it in GitHub Desktop.
Robotlegs inspired classes with Agility.js
/**
* Created by IntelliJ IDEA.
* User: Digital Keystone (gavin.jackson)
* User: gavin.jackson
* Date: 06/03/12
* Time: 11:24
*/
/**
* Class: AbstractCommand
*/
var AbstractCommand = $$(Actor,{
model: {name:"AbstractCommand"},
view: {},
controller: {},
//--------------------------------------------------------------------------
//
// Getters
//
//--------------------------------------------------------------------------
setName:function(name){this.model.set({name:name})},
getName:function(){return this.model.get("name")},
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
execute:function(){
//override in subclass
this.onSuccess();
},
/**
* Called by onSuccess() or onFailure(), nice place to remove handlers etc..
*/
onComplete:function(){
//override in subclass
},
onSuccess:function(){
trace("success " + this.getName() + " command");
this.onComplete();
this.trigger(CommandEvent.SUCCESS());
},
onFailure:function(){
this.onComplete();
this.trigger(CommandEvent.FAILURE());
}
});
/**
* Created by IntelliJ IDEA.
* User: Digital Keystone (gavin.jackson)
* User: gavin.jackson
* Date: 06/03/12
* Time: 15:09
*/
/**
* Class: AbstractCommandQueue extends AbstractCommand
*/
var AbstractCommandQueue = $$(AbstractCommand, {
model:{name:"AbstractCommandQueue", queue:[],currentCommand:null},
view:{},
controller:{
'create':function(){this.populateQueue()}
},
//--------------------------------------------------------------------------
//
// Getters
//
//--------------------------------------------------------------------------
setQueue:function(queue){this.model.set({queue:queue})},
getQueue:function(){return this.model.get("queue")},
setCurrentCommand:function(currentCommand){this.model.set({currentCommand:currentCommand})},
getCurrentCommand:function(){return this.model.get("currentCommand")},
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
//----------------------------------
// queue
//----------------------------------
addCommand:function(command){
this.getQueue().push(command);
},
populateQueue:function(){
/*
trace("populating queue");
//Override in sub-class
this.addCommand($$(DelayCommand,{name:"1 loz delay"},{},{}));
this.addCommand($$(DelayCommand,{name:"2 henry delay"},{},{}));
this.addCommand($$(DelayCommand,{name:"3 daisy delay"},{},{}));
*/
},
doNext:function(){
if(this.getCurrentCommand() != null)
{
//trace("Unbinding from " + this.getCurrentCommand().getName());
try
{
this.getCurrentCommand().unbind(CommandEvent.SUCCESS());
this.getCurrentCommand().unbind(CommandEvent.FAILURE());
}
catch(e)
{
//do nothing, known issue with JQuery apparently
//@see http://forum.jquery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jquery-1-4-2
}
this.setCurrentCommand(null);
}
if(this.getQueue().length==0)
{
//trace("Finished queue");
this.onSuccess();
}
else
{
this.setCurrentCommand(this.getQueue().pop());
//trace("Processing " + this.getCurrentCommand().getName());
this.getCurrentCommand().bind(CommandEvent.SUCCESS(),this.doNext);
this.getCurrentCommand().bind(CommandEvent.FAILURE(),this.reportFailureThenContinue);
this.getCurrentCommand().execute();
}
},
reportFailureThenContinue:function(){
traceError(this.getCurrentCommand().getName() + " failed, carrying on with command queue");
this.doNext();
},
//----------------------------------
// command pattern
//----------------------------------
execute:function(){
//trace("executing queue");
this.getQueue().reverse();
this.doNext();
}
});
/**
* Created by IntelliJ IDEA.
* User: Digital Keystone (gavin.jackson)
* User: gavin.jackson
* Date: 20/03/12
* Time: 11:34
*/
/**
* Class: AbstractContext extends Actor
*/
var AbstractContext = $$(Actor, {
model:{},
view:{},
controller: {
'~create':function(){this.dispatchOnBus(ContextEvent.START_UP())}
}
});
/**
* Created by IntelliJ IDEA.
* User: Digital Keystone (gavin.jackson)
* User: gavin.jackson
* Date: 06/03/12
* Time: 15:28
*/
/**
* Class: AbstractService
*/
var AbstractService = $$(Actor,{
model:{},
view:{},
controller:{},
onSuccess:function(){
this.trigger(ServiceEvent.SUCCESS());
},
onFailure:function(){
this.trigger(ServiceEvent.FAILURE());
}
});
/**
* Created by IntelliJ IDEA.
* User: Digital Keystone (gavin.jackson)
* User: gavin.jackson
* Date: 07/03/12
* Time: 09:59
*/
/**
* Class: Actor
*
* Core class for any framework object to be able to speak to any other
* framework object over the common event bus
*/
var Actor = $$({
model:{registeredEvents:[]},
view:{},
controller:{
'create':function(){this.onRegister()},
'destroy':function(){
try
{
for(var i=0; i<this.getRegisteredEvents().length;i++)
{
EventBus.unbind(this.getRegisteredEvents()[i]);
}
this.setRegisteredEvents([]);
}
catch(e)
{
//do nothing, weird JQuery bug
}
}
},
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
setRegisteredEvents:function(registeredEvents){this.model.set({registeredEvents:registeredEvents})},
getRegisteredEvents:function(){return this.model.get("registeredEvents")},
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
/**
* Use to listen to any framework events
*/
onRegister:function(){},
/**
* Dispatches the supplied event (string) over the common event bus that
* all Actors share, anything dispatched here, can be listened to by any
* other Actor using the addBusListener()
* @param event string describing namespaced event
* @param payload data to be sent with the event
*/
dispatchOnBus:function(event,payload){
EventBus.trigger(event,payload);
},
/**
* Use to listen to common events dispatched over the bus. Allows any Actor
* to talk to any other Actor.
* @param event string describing namespaced event
* @param handler function to handle event, First param MUST be event
*/
addBusListener:function(event, handler){
EventBus.bind(event, handler);
this.getRegisteredEvents().push(handler);
//TODO add oneShot handling
}
});
/**
* Created by IntelliJ IDEA.
* User: Digital Keystone (gavin.jackson)
* User: gavin.jackson
* Date: 07/03/12
* Time: 09:59
*/
/**
* Class: EventBus
*
* Common channel under which framework events are sent
*
*/
var EventBus = $$({
model:{},
view:{},
controller:{}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment