Contact Me


Recent Posts


Categories


Archives


Tags

3d actionscript Actionscript 2 Actionscript 3 api APIs as2 as3 brand caching carousel channel code compress contest contract CSS Flash flv fp10 free freelance gadget gadgets gdata google JavaScript jquery mvc papervision papervision3d PHP player playlist puremvc pv3d search tube tutorial twitter video widget XML you youtube

Recent Comments

  • Ahmed: Can you put up an example?
  • Ahmed: Hmmm sorry this is a tough one but I don’t work with ASP.NET! Sorry!
  • Michael: Good example but one question… in all the slider skinning I see with Flex the thumb overruns the left...
  • venkata: Hi I am using OAuth to connect to youtube api. I got accessToken and TokenSecret for a particular user. Now...
  • Ahmed: Good old flexlib, what component are you using?

Links


Help end world hunger

Speeding Up PureMVC Development, Part 2.1: The Mediator (2)

I recently wrote a post about speeding up development with PureMVC’s mediator. Now I’ve created a super mediator class that will defiantly help you speed up your code.

I mentioned before that I really didn’t like the idea of having to use “listNotificationInterests()” and then “handleNotification()” as these functions lead to unnecessary duplication of code and, if you’re like me, most of the time you use the “switch()” within “handleNotification()” to execute functions rather than writing a load code.

So what have I come up with you ask? Well, here’s my new mediator class (which you will extend instead of extending PureMVC’s regular mediator):

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * @author          Ahmed Nuaman (http://www.ahmednuaman.com)
 * @langversion     3
 *
 * This work is licenced under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
 * To view a copy of this licence, visit http://creativecommons.org/licenses/by-sa/2.0/uk/ or send a letter
 * to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
*/

package com.firestartermedia.lib.puremvc.patterns
{  
    import com.adobe.utils.ArrayUtil;
    import com.firestartermedia.lib.puremvc.events.SpriteEvent;
    import com.oneclicktofame.ApplicationFacade;
   
    import flash.utils.Dictionary;
   
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class Mediator extends org.puremvc.as3.patterns.mediator.Mediator
    {
        protected var notificationInterests:Array               = [ ];
        protected var notificationHandlers:Dictionary           = new Dictionary();
       
        public function Mediator(name:String=null, viewComponent:Object=null)
        {
            super( name, viewComponent );
           
            trackEvent( 'Registered ' + name );
        }
       
        public function trackEvent(event:String):void
        {
            sendNotification( ApplicationFacade.TRACK, event );
        }
               
        public function sendEvent(event:SpriteEvent):void
        {
            sendNotification( event.type, event.data );
        }
       
        public function declareNotificationInterest(notificationName:String, func:Function):void
        {
            notificationInterests.push( notificationName );
           
            notificationInterests = ArrayUtil.createUniqueCopy( notificationInterests );
           
            notificationHandlers[ notificationName ] = func;
        }
       
        override public function listNotificationInterests():Array
        {
            return notificationInterests;
        }
       
        override public function handleNotification(notification:INotification):void
        {
            notificationHandlers[ notification.getName() ].apply( null, [ notification ] );
        }
    }
}

The idea behind it is that you would now use “declareNotificationInterest()” where you would pass in the notification name and then the function to execute, like so:

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 com.oneclicktofame.view
{
    import com.firestartermedia.lib.puremvc.patterns.Mediator;
    import com.oneclicktofame.ApplicationFacade;
    import com.oneclicktofame.model.ConfigProxy;
    import com.oneclicktofame.model.VideoProxy;
    import com.oneclicktofame.view.component.PlayerView;
   
    import org.puremvc.as3.interfaces.INotification;

    public class PlayerViewMediator extends Mediator
    {
        public static const NAME:String                         = 'PlayerViewMediator';
       
        private var playerView:PlayerView;
       
        public function PlayerViewMediator(viewComponent:Object=null)
        {
            super( NAME, viewComponent );
           
            declareNotificationInterest( ApplicationFacade.RESIZE,      handleResize );
            declareNotificationInterest( PlayerView.PLAY,               handlePlayerViewPlay );
            declareNotificationInterest( PlayerView.PAUSE,              handlePlayerViewPause );
            declareNotificationInterest( PlayerView.RESUME,             handlePlayerViewResume );
            declareNotificationInterest( VideoProxy.VIDEO_DATA_READY,   handleVideoProxyVideoDataReady );
        }
        ...

It’s a bit of a shame that I have to place the functions within the constructor but it’s just how PureMVC works. I may try to suggest to the guys are PureMVC to see if there’s a possibility to delay the binding of notifications to the mediator within the “onRegister()” function.

However, this still works a charm and means that I don’t have to write half was much code!

If you want the source code, you can get out my repo from git: http://github.com/ahmednuaman/AS3/tree/master


Leave a Reply

Your comment:

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Fork me on GitHub