Mike Chambers

code = joy

MXNAAppController.as

with 8 comments

I am going to post the class files for the MXNA WebService App I put together over the weekend. I will post them as I add the comments.

I will eventually post all of the source, but I want to work out all of the kinks first.

The first class I am posting, MXNAAppController is the controller for the application. It handles all communication with the web services on the server, and contains a single view class, that contains most of the UI elements for the app.

So, if you find any bugs, or have any suggestions, post them in the comments.


import mx.containers.Window;
import com.macromedia.mxna.MXNAWebService;
import com.macromedia.mxna.app.MXNAFeedView;
import mx.controls.Alert;
import mx.controls.ProgressBar;

/* Associated with the main app controller clip */
class com.macromedia.mxna.app.MXNAAppController extends MovieClip
{
	/*Window Component for Background*/
	private var bgWindow:Window; 

	/*MXNA WebService Class to Load Data*/
    private var ws:MXNAWebService;
	/*Main App View*/
    private var feedView:MXNAFeedView;

	/*clip to block window component interaction*/
    private var blocker:MovieClip;

	/*Progress Bar Component*/
    private var pBar:ProgressBar;

	/*Used to cache category data*/
    private var feedCache:Object;
    private var so:SharedObject;

	/*
		Constructor. We do everything in onLoad to be sure
		everything has initialized
	*/
    public function MXNAAppController()
    {
    }

	/*
		Called by Flash when the clip is laoded on stage. We do a bunch of
		initialization here.
	*/
    private function onLoad():Void
    {
        feedCache = new Object();

		//set the clip to catch mouse events to the Window Component title bar.
		//we jsut want to use it for the background
        blocker._alpha = 0;
        blocker.onRelease = function(){};
        blocker.useHandCursor = false;

        bgWindow.title = "MXNA WebService Example";

		//braodcast when the user selects a category
        feedView.addEventListener("onSelectCategory", this);

		//Custom class that provides an interface / API to the MXNA webservices
        ws = new MXNAWebService();
		//event when categories load from server
        ws.addEventListener("onGetCategories", this);
		//event when posts load from server
        ws.addEventListener("onGetPostsByCategory", this);
		//event when an error occurs
		ws.addEventListener("onFault", this);

		//see if categories have been laoded before. If so, use them.
        so = SharedObject.getLocal("MXNAWebServiceExample");
        var categories = so.data.categories;

        if(categories != undefined)
        {
            setCategories(categories);
        }
        else
        {
            enableProgress(true, "Loading Categories...");
			//if categories not saved from before, then load them from the server.
            ws.getCategories();
        }
    }

    public function onActivate(Void):Void
    {
    }

	//utility methods that takes an array of categories and sets them in the UI
	//passing them to the feedView
    private function setCategories(categories:Array):Void
    {
        feedView.setCategories(categories, "CATEGORY");

		//when the categories are loaded, lets go get the first batch of posts
		//so the user has something to look at.
        getPostsByCategory("All");
    }

	//takes a category, and gets the posts for that category from the server
    private function getPostsByCategory(category:String):Void
    {
        enableProgress(true, "Loading " + category + " Feeds...");
        ws.getPostsByCategory(category);
    }

	//method to manage the progress bar.
	//We are usign it in indetermindate mode, so, we need to turn it off when
	//it is not being used (i.e. make it invisible).
    private function enableProgress(enabled:Boolean, label:String):Void
    {
        if(label == undefined)
        {
            label = "";
        }

        pBar.indeterminate = enabled;
        pBar.label = label;
        pBar.enabled = enabled;
        pBar._visible = enabled;      

		//if something is being loaded, we need to disable the UI in order to
		//keep things in sync
        feedView.enabled = !enabled;
    }

	//event listener method, called when categories are loaded from the server.
    private function onGetCategories(eventObj:Object):Void
    {
        enableProgress(false);

		//contains an array of string category names
        var results:Array = eventObj.categories;

		//we need to add the "All" option since this is not returned from
		//the server
        results.unshift({CATEGORY:"All"});

		//lets save the categories in an SO, so we don't have to load them next
		//time
        so.data.categories = results;
        so.flush();

        setCategories(results);
    }

	//event listener method, called when the user selects a category from the UI.
    private function onSelectCategory(eventObj:Object):Void
    {
		//category name
        var category:String = eventObj.category;

		//lets see if the feeds are in the cache
        if(feedCache[category] != undefined)
        {
			//if so, use it
            feedView.dataProvider = feedCache[category];
            return;
        }

		//otherwise, go to the server to get the feeds for the category
        getPostsByCategory(category);
    }

	//event listener method, called when posts are loade from the server
    private function onGetPostsByCategory(eventObj:Object):Void
    {
        enableProgress(false);   

		//add them to the cache.
        feedCache[eventObj.category] = eventObj.feeds;

		//send them to the feedView to be displayed
        feedView.dataProvider = eventObj.feeds;
    }

	//called if an error occurs. only broadcast by MXNA class right now.
    private function onFault(faultObj:Object):Void
    {    

	    var info:Object = faultObj.fault;

		//show an Alert box with some info
	    Alert.show(info.detail,
	    			info.faultstring,
				Alert.OK,
				this,
				null,
				"errorIcon",
				Alert.OK);

		feedView.enabled = false;
		enableProgress(false);

    }
}

Btw, big ups to Sean Voisen for the AS 2 support in his MovableType CodeBeautifier.

Written by mikechambers

January 27th, 2004 at 2:34 am

Posted in ActionScript

8 Responses to 'MXNAAppController.as'

Subscribe to comments with RSS

  1. seen’s fine, but where can I find the imports
    com.macromedia.mxna ?

    Marcos Neves

    27 Jan 04 at 9:37 am

  2. nice code, still have to look it over. I’m actually just gazing at the codebeautifier.. wow.. nice job sean.

    Dominick

    27 Jan 04 at 9:53 am

  3. >com.macromedia.mxna

    I havent posted these yet, as they are still be tweaked and commented.

    I will post the rest of the classes soon.

    mike chambers

    mesh@macromedia.com

    mike chambers

    27 Jan 04 at 12:54 pm

  4. Inspired by your great app I tried to built a custom WebService class by my own but I were not able to extend the built-in WebService class:

    // this fails
    class WebServiceEx extends WebService {}

    // this fails, too
    import mx.services.*;
    class WebServiceEx extends WebService {}

    The compiler keeps complaining that it can’t find the WebService class. I triple checked the classpath but it just won’t work.

    Any ideas?

    Dirk Eismann

    2 Feb 04 at 4:45 am

  5. you have to manually include the classes by following these steps:

    1. Window > Other Panels > Common Libraries > Classes
    2. Drag the WebServicesClasses Symbol from the library onto the stage.
    3. Delete the WebServicesClasses Symbol (steps 2 and 3 puts it in your library).

    mike chambers

    mesh@macromedia.com

    mike chambers

    2 Feb 04 at 1:20 pm

  6. Great Web News App.
    I would like to build my own web service,and class Files.
    How can I start learning how to do that?
    Any Info Would Help.

    Brandon

    19 Apr 04 at 2:23 am

  7. What about when you’re writing a class file and you can’t drag something onto the stage? How do you access the webservice classes?

    Jordan

    10 Sep 04 at 3:43 pm

  8. “What about when you’re writing a class file and you can’t drag something onto the stage? How do you access the webservice classes?”

    Yes how do you do this?

    David Lee

    12 Oct 04 at 5:32 pm

Leave a Reply