Mobile Flex: View Data

Posted on May 16, 2011 at 9:05 am in Development

From the previous post you should know how to navigate from 1 view to the next using the ViewNavigator.  Now, you want some data in that view right? No problem, this is where the View object’s ‘data‘ property comes into play. Setting the data property is accomplished by passing the data object, in addition to the View’s class name, into the pushView() method on the navigator object.

Example:

navigator.pushView(MyNewView, dataObject);

This effectively calls the setting for the data property of the new View (MyNewView) object that is created.

Managing View Data

You could work with the data property on the View object directly. For instance, if the data object passed into the View via the pushView() method was a simple user object that contained a name property, you could bind the name property to a label control.

Example:

<s:Label id="name_lbl" text="{data.name}" />

Overriding the Data Property Setter

Usually though, you’d want to override the setter for the data property. Then you can type your object and work with it in a better manner.

Example:

protected var user:User;
override public function set data(value:Object):void
{
 super.data = value;
 user = value as User;
}

 

<s:Label text="{user.name}" />

So now we’ve got the data in the view. The next step is to manage the state of each view. With mobile apps you can’t count on the view staying around, so we’ll need to keep a tight control on the state of each view. That way we can bring the user right back where they expect to be when they come back to the app after a call for example. In the next post we’ll look into how to do this. Stay tuned.

Tagged with , , ,

* Required


Comments

  1. steve 13 July 2011 at 10:43 am permalink

    Hi, I have a TabbedViewNavigatorApplication with 3 views(ViewNavigator),
    I can use the navigator.pushView(view,dataobject) from actionscript to navigate to another view with a data object of my choice.
    How do I pass a data object of my choice when the user clicks on the default tab button along the bottom to switch views?

    • John Crosby 13 July 2011 at 1:55 pm permalink

      Steve – As I understand it the TabbedViewNavigatorApplication controls the 3 ViewNavigators in your application. The tabs control the ViewNavigator being displayed and in turn the ViewNavigator display the actual view and the data for each view within the view stack. Is there a way that you can structure your data so it isn’t dependent on the tabs for control, but allows the the ViewNavigators to control the view data? At that point we talking apples to apples and can use the process described in this post.. If not, you might need to do some wonky-ness with data management and how the data is accessed based on the selctedIndex of the TabNavigator because there is not a built in way to deliver data to the views from that level of the application. Let me know if I’m not understanding your problem correctly, or if you have any further questions.