Good Tutorial for learning PureMVC with Sample Code:-
(url: http://codeofdoom.com/wordpress/2009/01/25/puremvc-sample/ )
(url: http://codeofdoom.com/wordpress/2009/01/25/puremvc-sample/ )
Well it has been awhile since I have written anything, but that doesn’t mean that I haven’t been busy. Lately at work I’ve been working a lot more closely with Flex, writing some pretty creative/new custom components, learning a lot, etc. Lately we discussed the possibilities of switching from the standard use of Cairngorm to the use a PureMVC, so I wanted to go over a brief tutorial of PureMVC to show how its used. Later I will be posting a short article over the differences of cairngorm vs PureMVC.
First, what is PureMVC? It’s an open source MVC (Model, View, Component) framework that has now been ported over to 10 different languages (latest has been to Objective C for iPhone dev) that really forces you follow the rules of MVC. It allows you to separate out all of your logic of retrieving your data, for what to do with that data, what components get what, etc all through one central facade object. Now this tutorial will be focusing on how it works with the singleton core, but they also have the notion of a “multiton”, which is basically a map of singletons.
First, what is PureMVC? It’s an open source MVC (Model, View, Component) framework that has now been ported over to 10 different languages (latest has been to Objective C for iPhone dev) that really forces you follow the rules of MVC. It allows you to separate out all of your logic of retrieving your data, for what to do with that data, what components get what, etc all through one central facade object. Now this tutorial will be focusing on how it works with the singleton core, but they also have the notion of a “multiton”, which is basically a map of singletons.
Quick run down of the 4 parts.
- Proxies are used to manage your data object. It will be the one that requests your data. Once the data returns, it will then fire off a notification stating that the data has returned.
- Mediators are used to managed your components. This also picks up notifications of data and that is where you will set the data to your components.
- Commands are mostly there to interact with the Proxies and Mediators
- Facade is the single interface (also are a singleton) used to manage the communications of your application
So lets start diving into some code. In this example, I am basically using a HTTPService to load an rss feed into a datagrid.
First things first, we grab an instance of our facade and pass in the components we want to register with it. This is within the PureMvcExample.mxml file.
When you first initialize the facade, you will notice there is an initializeController method called whenever you do super() in the constructor. This is where it will register your command to start the application.
As you see, the StartUpCommand just extends Command and is used to registered both the proxies and the mediators with the facade. Nothing too special.
When we initialize MyProxy, you will notice that it will call setup(). This is what we use to setup the service call. We add an eventlistener to it for when data comes back and thats where some fun stuff goes on. Now that our proxy is initialized, the next line in the StartUpCommand is to initialize the mediator.
Now whenever we register a mediator, within PureMVC, it will call the listNotificationInterests method. This is what tells PureMVC which events that this specific mediator cares about. For our example, we only care about PureMvcConstants.DATA_LOADED. Inside the constructor, we then call loading that calls MyProxy to go retrieve the data. Note that we are receiving the proxy through the facade. Typically you would have an interface that you would cast this to in a real application and not cast MyProxy directly, but this is just an example.
So lets look back at MyProxy.
The loadInfo() just has a _service.send() call. After that our eventlistener that we setup before picks it up. All that does is just takes the data uses the facade to fire off the notification that “here is some data” (under the fancy name PureMvcConstants.DATA_LOADED) . But who is listening?
Recall earlier that our mediator listed PureMvcConstants.DATA_LOADED as an “interest”.
So if you go back to MyMediator, there is a handleNotificationMethod that we have overridden.
This is where we take our data back that was sent off with the notification and apply it to our viewComponent.
You might not notice it, but I feel the mediator level is what really makes PureMvc special. We have now separated out application specific code into their own class. With this extra layer, if you wanted to swap out what happens when data comes back in the application, all you have to do is change the mediator. You dont have to worry about writing that code within the viewComponent itself anymore.
Now this is a small example of how the flow of a PureMvc application works, but normally you would pass in a higher level component as your viewComponent into the mediator. Something that will contain 1 or more actual components within it. If you didnt take that route, then you are talking about having proxys and mediators for everything. I suppose that is doable, but it is probably not recommended.
Here is the source
No comments:
Post a Comment