How does the controller know about each command class? Is each instantiated by
the controller?
Yup, controller knows about command classes.
This is the FrontController (part of my MVC framework), which the AppController
extends.
Might be able to view it in my repository, allthough i'm not sure if you have
access as a non-assembla member
http://www.assembla.com/code/muzakdeezign/subversion/nodes/trunk/AirMVC/src/be/gip/mvc/control/FrontController.as?rev=67
=============================================================
private var commands:Dictionary = new Dictionary();
public function addCommand(eventType:String, commandRef:Class):void {
if(commands[eventType] != null) {
throw new MVCError("The command has already been registered: "+eventType);
}
this.commands[eventType] = commandRef;
MVCEventDispatcher.getInstance().addEventListener(eventType,
this.executeCommand, false, 0, true);
}
protected function executeCommand(event:MVCEvent):void {
var commandToInitialise:Class = this.getCommand(event.type);
var commandToExecute:ICommand = new commandToInitialise();
commandToExecute.execute(event);
}
protected function getCommand(commandName:String):Class {
var command:Class = this.commands[commandName];
if (command == null) {
throw new MVCError("A command with the given name does not exist: " +
commandName);
}
return command;
}
================================================================
So in each project i have 1 AppController (that extends FrontController) which
maps Events to Commands.
Do the command classes talk directly to the model or view or back through the
controller?
Command classes talk directly to the model, so they know the model, they don't
know about views.
Changes in the Model trigger an event, which a View may (or may not) listen to.
Only the Controller knows about commands.. and it doesn't "really know" them. All it
knows is they have an "execute" method.
So basically,
- view triggers event
- controller listens for event, looks up command mapped to that event
- if command is found, an instance of that command is created and its
execute() method is invoked.
- command does whatever it needs doing (like fetch data from server)
- command stores data in model, which triggers a change event
- view listens for model change event(s)
Event broadcasting/listening is done through an EventDispatcher Singleton:
http://www.assembla.com/code/muzakdeezign/subversion/nodes/trunk/AirMVC/src/be/gip/mvc/events/MVCEventDispatcher.as?rev=67
All of this is basically a mixture of ARP and Cairngorm.
regards,
Muzak
----- Original Message -----
From: "Mattheis, Erik (MIN-WSW)" <[email protected]>
To: "Flash Coders List" <[email protected]>
Sent: Thursday, March 15, 2012 11:10 PM
Subject: Re: [Flashcoders] Dispatching events from V to C
How does the controller know about each command class? Is each instantiated by the controller? Do the command classes talk directly
to the model or view or back through the controller?
On 3/15/12 4:44 PM, "Peter Ginneberge" <[email protected]> wrote:
==============================================
The controller then maps the different event types to Commands:
// AppController:
private function initCommands():void {
addCommand(NewsEvent.GET_NEWS, GetNewsCommand);
addCommand(NewsEvent.UPDATE_NEWS, UpdateNewsCommand);
addCommand(NewsEvent.ADD_NEWS, AddNewsCommand);
addCommand(NewsEvent.DELETE_NEWS, DeleteNewsCommand);
// etc..
}
==============================================
addCommand (not included) does an addEventListener and maps it to the command
class for later execution (when the event is
triggered).
_ _ _
Erik Mattheis | Weber Shandwick
P: (952) 346.6610
M: (612) 377.2272
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders