Perhaps this is because we’re using a slightly older Cairngorm, but one of my coworkers informed me yesterday that you can’t have more than one command per event in a cairngorm app. Seems like an odd limitation. I guess that’s one more reason for me to go the parsley MVC route in the future.
This bit me in the rear when I first started with Cairngorm, and still tweaks me something silly.
You _can_ have more than one command per event, but the must be registered in separate controllers. I’ve worked around this by mapping an event to an “aggregate” command that creates and runs the N other commands involved. Nasty stuff, and a design flaw in Cairngorm IMHO.
You may also want to check out the UM Cairngorm extensions:
http://code.google.com/p/flexcairngorm/
Thanks guys, I’ll make sure to look into UM Cairngorm, this is the first I heard of it.
Good idea bout the multiple controllers, that would work for this.
So can you give a use-case as to where you’d want an event to fire more than one command ? It’s not a flaw in the design as much as an intention of the design; that user-gestures are represented by commands, and that commands are responsible for orchestrating all of the work related to that user-gesture. A command is free to employ the use of all manner of other classes; perhaps you’re artificially limiting yourself to trying to stick all business logic in commands, and not thinking about creating non-Cairngorm classes to participate in your design ?
Multiple controllers is a bad smell as well for me; unless the commands the controller is handling are completely exclusive of each other (which to me would be refactoring towards 2 separate applications/modules). The whole point of the controller is a single switch to decide on how to handle a user-gesture; if you have multiplicity in your controllers, the extreme case (1 controller per event) has now just made your controller redundant.
Use-cases would help, code would help even more, in helping to understand what you’re trying to achieve, and whether Cairngorm is preventing you actually, or as I suspect, artificially.
It’s ok to colour outside the lines.
You can also use the SequenceCommand in Cairngorm to chain multiple commands to a single event.
While you would still have to register an event for each of the commands, you would only have to dispatch one event to automatically cause the rest of the commands to be executed.
The use-case my co-worker was trying to do was add a document modified flag to the UI. He wanted to hook up a single command to display that flag to any event that modifies the document. He ended up making that flag a property of the model that the model set automatically set on change (which was probably a better solution).
Another use case might be if you have 3 buttons. The first two each perform a different action. The third button performs both of those actions and is provided as a shortcut for the user. I hadn’t seen the SequenceCommand before, so that could be used to solve this. But perhaps a more elegant solution would be to hook up that third event to the first two commands.
This is the first time we’ve run into this, it’s certainly not an often-needed feature.
It’s funny to see which blog posts end up getting the most feedback…