Contact Me


Recent Posts


Categories


Archives


Tags


Recent Comments

  • Ahmed: Anything I can do to help, I need to update this plugin, need to implement oembed too
  • Phil: Currently not in use on the live site (given) but will be soon as it all works in development while renovating...
  • Ahmed: Yeah, I’ll come to writing stuff up one day! Sorry for the lack of docs, it takes time and I know I...
  • Ahmed: What you could do now is migrate to the new AS3 API: http://ahmednuaman.com/blog...
  • Ahmed: Of course. I’ll put one up ASAP, but all you have to do is to post to YouTube in your current page. With...

Links


Help end world hunger

Speeding Up PureMVC Development, Part 1: The View

After my tutorial on using and understanding PureMVC, I got around to write some classes that extended some of the core classes of PureMVC.

The main thing that got to me was the repetition of functions such as functions that send events from a view to the mediator and so on. I think it’s just much better if you can get away with creating a nice set of classes that extend the core and allow you to code that much better, so here they are:

Extending the View

The view is very important, without it, there wouldn’t be a UI! The main thing that got me about PureMVC’s views was that they needed to bubble events up to their mediators, it meant that each view needed a function like this:

1
2
3
4
public function sendEvent(eventName:String, body:Object=null):void
{
    dispatchEvent( new SpriteEvent( eventName, body, true ) );
}

It just seemed a shame to have something like that repeated in each view, so I created an extension to the “Sprite” class that’s perfect for PureMVC, here it is:

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
package com.firestartermedia.lib.puremvc.display
{
    import com.firestartermedia.lib.puremvc.events.SpriteEvent;
   
    import flash.display.Sprite;

    public class Sprite extends flash.display.Sprite
    {
        public var registered:Boolean                           = false;   
        public var ready:Boolean                                = false;
        public var tweenResize:Boolean                          = false;
       
        public function sendEvent(eventName:String, body:Object=null):void
        {
            dispatchEvent( new SpriteEvent( eventName, body, true ) );
        }
       
        public function sendReady(eventName:String):void
        {
            ready = true;
           
            sendEvent( eventName );
        }
       
        public function handleResize(e:Object):void
        {
           
        }  
    }
}

As you can see that the “sendEvent()” function is all there for you. There’s also other functions call “sendReady()” and “handleResize()”, you can use “sendReady()” to tell your mediator that the view has finished creating itself and is now ready for some commands, and the “handleResize()” function kind of teaches you to standardise your naming convention for resizing functions in your views and also sets you up to make sure that you write resizing commands.

There are also a few public functions: “registered”, “ready” and “tweenResize”. I use “registered” to check if the view has been created, therefore in the view’s constructor I set “registered = true”. The “ready” variable is a nice way for your mediator to check if your view is ready, if not, it can just defer calls ’til it is. And “tweenResize”, another one for me, allows my mediator to check if the resizing function needs to call a tween or not. Quite simple and very helpful!

Tell me what you think, if you find it useful! More to follow too!


2 Responses to “Speeding Up PureMVC Development, Part 1: The View”

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