Kote is a lightweight and easy to use MVC framework for ActionScript and Flex.
Kote brings together the best features from PureMVC and as3-signals
Notification represents an event that occure in your application.
Facade is the central part of the framework. It manages mediators, proxies and commands. This is a good place to list your notifications.
public class ApplicationFacade extends Facade { public static const CHAT_MESSAGE_RECEIVED:Notification = new Notification("chat message received"); public static const SEND_CHAT_MESSAGE:Notification = new Notification("send chat message");
public function ApplicationFacade() { addCommand(SEND_CHAT_MESSAGE, new SendChatMessageCommand());
addProxy(new ChatServiceProxy()); addMediator(new ChatLogMediator()); } }
Commands are the controller of your application. Any command is an object that handles notification. This is part of controller of your application. Typical command has run()
method which accepts arguments that were specified when the notification sent.
public class SendChatMessageCommand extends Command
{
public function run(receiver:Person, message:String):void
{
ChatService.instance.pushMessage(receiver, message);
}
}
Now we can send a notification like this:
sendNotification(ApplicationFacade.SEND_CHAT_MESSAGE, myCat, "Hello, Kote!");
Command can also abort notification. When notification aborted, no more commands will be executed and subscribed mediators will not receive it.
To abort notification override command’s execute()
method to return false:
override public function execute(notificationObject:NotificationObject):Boolean
{
notificationObject.parameters = ["this notification is aborted"];
return false;
}
Note that argument type of execute() method is NotificationObject, which allow to change notification parameters.
Proxy represents a model of your application.
public class ChatServiceProxy extends Proxy
{
public function pushMessage(receiver:Person, message:String):void;
}
When ChatServiceProxy
receives a message from server, it can send a notification:
private function handleServerMessage()
{
sendNotification(ApplicationFacade.CHAT_MESSAGE_RECEIVED, myCat, "Kote atakue!");
}
Mediators represent view. A mediator usually controls a visual object. It can subscribe for notifications.
To handle a notification in mediator subscribe for it and create a handler function:
public class ChatLogMediator extends Mediator {
public function ChatLogMediator() { subscribe(ApplicationFacade.CHAT_MESSAGE_RECEIVED, handleChatMessage); }
private function handleChatMessage(sender:Person, message:String):void { // show message } }
Note that each handler should accept the same type of arguments that were passed to the notification.
Notifications also have signals (similar to Robert Penner’s as3-signals):
onSubscribe
onUnsubscrive
onAdd
onRemove
To listen for a singnal just add listener for it:
public function ChatLogMediator() { onSubscribe.addCallback(handleSubscribe); onRemove.addCallback(handleRemove); }
private function handleSubscribe(mediator:Mediator, notification:Notification):void { trace("Mediator has subscribed for", notification); }
private function handleRemove(mediator:Mediator, facade:Facade):void { trace("Mediator was removed from facade", facade); }