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

DawnCommands

sammyt edited this page Sep 13, 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 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 is 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 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 trigger 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 that command, because that is the type of the execute argument.

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 command to strings!

Also worth reading my blog post on all this :)

Clone this wiki locally