Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

DawnCommands

philostler edited this page Oct 26, 2010 · 5 revisions

Simple tools made simple!

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 you recognise.

Lets 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 in the app would be 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, that’s 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 it’s 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 it is GetMonkeysForTree that should trigger the command, because that is the type of the execute parameter.

It’s another type safely win for Dawn!! Dawn used the type of the parameter to create the mapping between the notification and the command… no more nasty mapping commands to strings!

It’s also worth reading my blog post on all this :)

Clone this wiki locally