Question

I'm getting some PureMVC experience and I want to use keyboard commands to control my view. The rest of the application doesn't need to know about what this view is doing.

Should I put them directly in the view, or should they be some place else and have the view be notified using a Notification when a key is pressed?

Thanks!

Was it helpful?

Solution

As you said, you have two alternatives - to put some listeners in the view.mxml class, or to put the listeners in some general class.

1-st - this seems to be the normal approach, no further explanations needed, every programmer will do the same.

2-nd approach is more interesting. If you have many views, listening for keyboard events, you will start using something like

public class EnterButtonPressed extends SimpleCommand 
{
  function execute(...):void
  {
    //do something with the model, and then notify the view
  }
}

but after adding more views which are supposed to listen for Enter key your class will end up like that

public class EnterButtonPressed extends SimpleCommand {
  function execute(...):void
  {
    switch(viewType)
    {
      case view1:
        //do something with the model, and then notify view1
        break;
      case view2:
        //do something with the model, and then notify view2
        break;
      case view3:
        //do something with the model, and then notify view3
        break;
      case view4:
        //do something with the model, and then notify view4
        break;
      ...
  }
}

Which seems awful if you listens to many keyboard events. But if you are familiar to design patterns you can use State Pattern.

It helped me a lot in my latest project, when I encountered many different view states listening for many events.

I also recommend you to take a look at Mate framework, it is like PureMVC + data binding + Flex events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top