Yordan Yanakiev
Yordan Yanakiev

Reputation: 2604

Access object inside View from another View?

applicationX.mxml :

<?xml version="1.0" encoding="utf-8"?>
<s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                  xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" >

    <s:ViewNavigator label="Login"    width="100%" height="100%" firstView="views.LoginView"    />
    <s:ViewNavigator label="Settings" width="100%" height="100%" firstView="views.SettingsView" />

</s:TabbedViewNavigatorApplication>

Settings.mxm ( Settings View ) :

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" >

    <s:Label id="myLabel" />
</View>

Upvotes: 0

Views: 239

Answers (2)

Jason Reeves
Jason Reeves

Reputation: 1716

you shouldn't. One view should never know what is inside another view. What you want is a model with a property like .loginStatus that can then be set by Login and seen by Settings. You can use one of many MVC styles to accomplish this. Do some googling on MVC patterns and Flex and see the different ways this is done. In the meantime, here is a quick for instance:

Settings.mxml:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" >

    <fx:Script>
        <![CDATA[
            private var model:MyModel = MyModel.getInstance();
        ]]>
    </fx:Script>

    <s:Label id="myLabel" text="{model.loggedInStatus}" />
</View>

Login.mxml:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" >

    <fx:Script>
        <![CDATA[
            private var model:MyModel = MyModel.getInstance();

            private function loginSucceded():void{
                model.loggedInStatus="Logged In";
            }
        ]]>
    </fx:Script>

</View>

MyModel.as

Singleton class with a property named .loggedInStatus. You can check this other answer AS3 singleton implementations for a discussion about various singleton patterns and why I use the one I use.

This is a VERY simple example. You wouldn't want to use a human readable string loggedInStatus to determine state or anything. but this is an example of how the model works and how views can display proper things based on the state of the model.

Upvotes: 1

csomakk
csomakk

Reputation: 5497

Set settings singleton. This means, you define a static variable pointing to the item itself (it is required to have only one instance of Settings.mxml)

add

public static var instance:Settings;

to settings. add initializeEventListener to settings, and inside the function set the instance:

instance=this;

Than you can access Settings page anytime by getting the singleton, like:

Settings.instance.myLabel.text="success";

Upvotes: 1

Related Questions