Ahmed Nuaman


builder of internets ~ developer of dreams ~ tamer of Dachshunds

Speeding Up PureMVC Development, Part 2: The Mediator

Update: I’ve updated the mediator class now, here’s the post: Speeding Up PureMVC Development, Part 2.1: The Mediator (2)

Following my post about speeding up PureMVC development regarding the view, I’m now moving on to part two of my series on PureMVC: The Mediator.

It’s handy if you understand and/or have used PureMVC before, if not, have a read of my tutorial on using and understanding PureMVC.

Extending the Mediator

As we know the mediator is very important, without it our views couldn’t interact with or be updated by our application. However, there are times when I do find that I’m just rewriting code between every mediator class, for example, when it came to handling events from the view, I found it annoying that sometimes I had to create handlers where all I wanted to do was just bubble the event via the mediator.

The solution is simple, just create a function that takes the event from the view and bubbles it up, much like:

public function sendEvent(event:SpriteEvent):void
{
    sendNotification( event.type, event.data );
}

Piece of pie eh? But then I found myself wanting to write this in ever mediator, but there’s no point to that, so I just extended the mediator class and then extend my class when writing a new mediator, here it is:

package com.firestartermedia.lib.puremvc.patterns
{  
    import com.firestartermedia.lib.puremvc.events.SpriteEvent;
   
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class Mediator extends org.puremvc.as3.patterns.mediator.Mediator
    {
        public function Mediator(name:String=null, viewComponent:Object=null)
        {
            super( name, viewComponent );
        }
       
        public function sendEvent(event:SpriteEvent):void
        {
            sendNotification( event.type, event.data );
        }  
    }
}

It’s not much of an extension, but it does help with the old coding. There are also further changes that I’m planning to make to the mediator class, for example, why do we have to use “listNotificationInterests()” and then duplicate code when setting “handleNotification()”? It’ll be much easier if we could combine these in a nice function, something I’ll hope to code up soon.

Tell me what you think.

Where have comments gone?

Good question my old fruity. I'm now sticking any post discussions on Google+. Why? Well simply it's better. WordPress's comment system isn't very elegant and nor are ones like Disqus or Livefyre, so to save hassle I've just shipped them off to a social network, like a real boss.

Search

My social skills

Latest blog posts

  • Loading posts...

Subscribe in a reader

Latest tweets

  • Loading tweets...