-
Notifications
You must be signed in to change notification settings - Fork 4
DawnCommands
Lets jump straight in and look at an example. Dawn commands are built on top of the injector and the notification bus, so expect to see some things your recognise.
Let say we have a view which displays a list of all the monkeys in a tree! From time to time the app needs to get a new list of monkeys in the tree (monkeys move about, and stuff).
Somewhere the app would have a line like this, which sends out a notification (using the INotificationBus
)
bus.trigger(new GetMonkeysForTree(tree));
Lovely, lets also see what that notification looks like
class GetMonkeysForTree { public var tree:Tree; public function GetMonkeysForTree(tree:Tree) { this.tree = tree; } }
Oooo, Its just like any other dawn notification, simple.
Now we want to trigger a command to get those monkeys! So how about that command
class GetMonkeysCommand { [Inject] service:MonkeyService; [Execute] public function execute(note:GetMonkeysForTree):void { service.getMonkeys(note.tree); } }
Phew, Thats our command written! Notice how it doesn’t implement an ICommand
interface, why would it want to? That would force it to cast the argument of its execute method from something like Event
or INotification
, and that would be a type safety fail!
Also we can see that the command uses a MonkeyService
instance, and it doesn’t have to do anything horrible like use a service locator to get hold of it, it can just ask Dawn to inject it.. woo commands are injectable!
ok, so we have a command and we have a notification. All that is left is the one line of configuration… but its not like command configuration you are used to.
commandMap.addCommand(GetMonkeysCommand);
Humm, look fishy? Notice how at no point did we have to tell Dawn that when the GetMonkeysForTree
notification is triggered its the GetMonkeysCommand
that should be invoked? All we had to do was tell dawn (via the ICommandMap
interface) that GetMonkeysCommand
is a command.
Dawn knows that its GetMonkeysForTree
that should trigger the command, because that is the type of the execute parameter.
Its another type safely win for dawn!! Dawn used the type of the parameter to create the mapping between the notification and the command… not more nasty mapping commands to strings!
Also worth reading my blog post on all this :)