We will pickup from where we left in the last article http://brupp.com/blog/2009/09/cairngorm-getting-started-part-1/. So lets redesign the feedreader application built in one of the previous post (http://brupp.com/blog/2009/09/feed-reader-in-flex/). Before continuing I ll suggest you to go through the previous two posts I have mentioned or else you will feel off track. At the end of the post you will have fairly good understanding on ViewHelper.
Create a new MXML application file which will be the “default” application file in our case. For the sake of understanding we will name it “FeedreaderUsingCairngorn.mxml“. This mxml is the main view of the application for which we will have a viewHelper, where all the view interaction code will be written. Lets call it “FeedreaderViewHelper.as”
Download the complete example from here.
package com.brupp.view
{
public class FeedreaderViewHelper extends ViewHelper
{
public function FeedreaderViewHelper()
{
super();
}
}
}
Note the helper class should extend the Cairngorm’s ViewHelper. Next is how to provide the “view” access to this helper class. It’s dead simple, just instantiate the helper object in the “FeedreaderUsingCairngorn.mxml” file.
<view:FeedreaderViewHelper id="viewHelper" />
Now the catch is the “id” that you give to the helper that actually serves as an identification to locate the helper from any class through the Singleton class “ViewLocator” (part of Cairngorm architecture). What happens is that the helper thus instantiated registers its identity mapped with its “id”, while doing so the “ViewHelper.as” class (Cairngorm framework class which is the parent class of our FeedreaderViewHelper) also stores the instance of “FeedreaderUsingCairngorn” in a property named “view” from where it was created. This is an internal mechanism (Will discuss in Advance Cairngorm tutorial series). So now our helper class can access the main view through “view” property like this:
package com.brupp.view
{
public class FeedreaderViewHelper extends ViewHelper
{
public function FeedreaderViewHelper()
{
super();
}
...
public function onItemClick(e:ListEvent):void
{
FeedreaderUsingCairngorm(this.view).....
}
...
}
}
Now once you have access to the view all the public objects can be accessed by helper; this way you dis-tangle the code from design. A viewhelper can serve multiple related views. Say if the main view has a sub-view called “FeedreaderView.mxml” then this view can have its method defined in the “FeedreaderViewHelper.as” itself. So we will put all our design elements that we created in our “Feedreader” post earlier here with only change that its events are now handled in FeedreaderViewHelper.
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" >
<mx:Script>
<![CDATA[
import com.brupp.view.FeedreaderViewHelper;
import com.adobe.cairngorm.view.ViewLocator;
[Bindable]
private var _objViewHelper:FeedreaderViewHelper;
public function onCreationComplete():void
{
_objViewHelper = ViewLocator.getInstance().getViewHelper("viewHelper") as FeedreaderViewHelper;
}
]]>
</mx:Script>
...
<mx:DataGrid id="dgFeed" width="100%" height="100%"
itemClick="{_objViewHelper.onItemClick(event)}"
doubleClickEnabled="true"
itemDoubleClick="{_objViewHelper.onItemDoubleClick(event)}">
...
</mx:VBox>
Two important things (in bold) to note. Notice in the code above how the helper is accessed and how the event handling is passed to helper. So just to get started with Cairngorm you need a view and a viewHelper and voila you started digging into the framework slowly.
Summarizing ViewHelper (taken from Cairngorm API documentation) :
ViewHelper is used to isolate command classes from the implementation details of a view. Model-View-Controller (MVC) best practices specify that command classes should interact with the view using the model (see the ModelLocator class), but, in some instances, command classes may require to both interrogate and update the view directly. Prior to performing any business logic, the command class may require to fetch values that have been set on the view; following completion of any business logic, the final task may be for a command class to update the View (user interface) with any results returned, or perhaps to switch the View entirely (to a different screen).
By encapsulating all the logic necessary for interrogating and updating a particular View into a single helper class, we remove the need for the command classes to have any knowledge about the implementation of the View. The ViewHelper class decouples our presentation from the control of the application.
A ViewHelper belongs to a particular View in the application; when a ViewHelper is created, its id is used to register against a particular View component (such as a particular tab in a TabNavigator, or a particular screen in a ViewStack). The developer then uses the ViewLocator to locate the particular ViewHelper for interrogation or update of a particular View.
I’m sure a lot of above must have started making sense to you now except the “Command” stuff. Right? Keep hooked to my future posts. So going back to the feedreader example we now need an event to call the web-service to populate our grid. CairngormEvent, Command and FrontController conjunction serves the purpose and we will see how in the next post.
If you liked this post, please spread the word. Share and cheer!!!.
If you need more information on above post drop a comment. Your comments are valuable and helps in molding the article so do share in.
Recent Comments