SignalCommandMap extension, a pure AS3 example

Currently busy learning the Starling framework; but as I recently decided to apply Joel Hooks SignalCommandMap extension to a project I was working on, I decided to do a quick typeup slash demo project of this extension. The demo which can be downloaded here as a zipped project is not Flex based in difference to the original demo located here.

This extension been around for a long time so no big news in this post, just providing a pure AS3 example.

The SignalCommandMap extension let’s you eliminate all native Flash Events in your application and since Object rather than String based, ripe the benefits of not having to deal with String based Custom Events. (Not long ago I had two custom events not working cause despite differently named constants the string identifier by mishap was identical, a genre…)

Below is a minimalist example of the SignalCommandMap extension at work. Events are passed to the framework from the NavigationViewMediator using a Signal that carries a ValueObject which tells wether the User selected a tomato or an orange. A Command is mapped to this Signal and when recieved, sets the data upon the Model which notifies the Framework. As a result the ContentView which holds a ScrollPane updates with either a tomato or an orange:

Get Adobe Flash player

This is the code for the NavigationViewMediator. As you can see it dispatches the ValueObject as a parameter: addItem.dispatch(action) where action is our ValueObject:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package view.mediator {
   import model.vo.ItemVO;
   import org.robotlegs.mvcs.Mediator;
   import signals.AddItem;
   import view.component.NavigationView;

   public class NavigationViewMediator extends Mediator {

      [Inject]
      public var view:NavigationView;

      [Inject]
      public var addItem:AddItem;

      public function NavigationViewMediator() {
         super();
      }

      override public function onRegister():void {
         this.view.itemSelected.add(handleItemSelected);
      }

      protected function handleItemSelected(action:ItemVO):void {
         addItem.dispatch(action);
      }
   }
}

The Signal itself is pretty simple. Note that in the super constructor we pass the relevant ValueObject:

1
2
3
4
5
6
7
8
9
package signals {
   import model.vo.ItemVO;
   import org.osflash.signals.Signal;
   public class AddItem extends Signal {
      public function AddItem() {
         super(ItemVO);
      }
   }
}

The Command mapped to the Signal is mapped inside another Command or could (as in the original example – and perhaps better you could say) be mapped directly in the Context. To be able to map a Signal to a Command in a Command, as I like to use Commands to bootstrap my application, all we need to do however is to make sure that the Command extends the SignalCommand class:

1
2
3
4
5
6
7
8
9
10
11
package controller.command {
   import org.robotlegs.mvcs.SignalCommand;
   import signals.AddItem;

   public class PrepSignalCommand extends SignalCommand {
      override public function execute():void {
         injector.mapSingleton(AddItem);
         signalCommandMap.mapSignalClass(AddItem, AddItemCommand);
      }
   }
}

And that’s it! I recommend a download of the original example by Joel Hooks to see the difference perhaps, and a download of my example in case you want to take the pure AS3 approach!

This post is now included in the robotlegs knowledgebase here
http://knowledge.robotlegs.org/discussions/resources/33-links-to-robotlegs-resources-examples-tutorials

Small note and afterthought: I wrote this pretty fast so in effect ideally I would not have had the images embed in the View class but be set in VO from Model…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 173,324 bad guys.