We help IT Professionals succeed at work.

How to get back to the orginal ViewStack after calling a Component

sfletcher1959
on
Medium Priority
477 Views
Last Modified: 2013-11-11
I have created a small application using ViewStack to switch between a dataGrid and an Edit Form. All seems to work ok except I can't figure out out to get back to a ViewStack defined in my main.mxml file from my EditView.

My Save button saves the data to the application server but it doesn't return to the ViewOnly view defined in the main.mxml file. My Cancel button doesn't work at all. :-)

I just can't seem to find a code example of how to do this.
Here are the buttons from my main.mxml file:
 
 <mx:HBox borderStyle="solid" paddingTop="10" paddingBottom="10" 
               paddingLeft="10" paddingRight="10">
                        <mx:Button label="View" click="goToPodDashboard();"/>                        
                        <mx:Button label="Edit" click="myViewStack.selectedChild=EditView;"/>
                        <mx:Button label="Delete"/>
                        <mx:Button label="Exit" click="returnToMagicHome();"/>
                    </mx:HBox>
 
 
Over on my editChartView.mxml file here is my buttons I have defined:
 
 
	<mx:ControlBar id="controlbar1" horizontalAlign="right" verticalAlign="bottom">
			<mx:Button id="searchButton" label="Cancel" click=click="myViewStack.selectedChild=ViewOnly;"/>    
            <mx:Button id="cInfoButton" label="Save" click="submitEdit()"/>    
</mx:ControlBar>

Open in new window

Comment
Watch Question

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Is the variable "myViewStack" known in editChartView.mxml?
Can you post where you initialize that variable?
Where's that view stack defined?
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
All you want is to get the reference of your view stack?

then in editChartView.mxml use the following

var app:main = this.parentApplciation as main;
app.myViewStack; // will be accessible.

Author

Commented:
The variable "myViewStack" is defined in the main.mxml file. So you are saying that any called components no nothing of the variables defined in main?

This is the only definition of myViewStack and it is in the main.mxml file:

 <mx:ViewStack id="myViewStack" width="100%" height="100%">

This is how I get to the editChartView.mxml:

<mx:VBox id="EditView">      
             <view:editChartView id="editChartView"
                   chart="{dG1.selectedItem}"            
                  resizeEffect="{fadeAndResize}"
                  horizontalScrollPolicy="off"
                  verticalScrollPolicy="off" height="100%">
            </view:editChartView>            
       </mx:VBox>

So what I am beginning to understand is that I have to instantiate "myViewStack" in the editChartView.mxml

I am not sure how to do it. I followed the example by shivaspk but I received the following errors:


1119: Access of possibly undefined property parentApplciation through a reference

1120: Access of undefined property app.

Thanks!


 

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> The variable "myViewStack" is defined in the main.mxml file
So, it's local to that file. It is not known by other mxml files.

If you want other classes to know it, you can pass it to them:

in editChartView.mxml

 
  private _viewStack:ViewStack;

  [Bindable]
  public function set viewStack(viewStack:ViewStack):void {
       _viewStack = viewStack;
  }
  public function get viewStack():ViewStack {
       return _viewStack;
  }

Then you can write in your main:

<view:editChartView id="editChartView"  viewStack={myViewStack}
                  chart="{dG1.selectedItem}"
                  resizeEffect="{fadeAndResize}"
                  horizontalScrollPolicy="off"
                  verticalScrollPolicy="off" height="100%">
</view:editChartView>

then inside editChartView you can use _viewStack (or viewStack since you have a getter too) like:

            click="_viewStack.selectedChild=ViewOnly;"
Principal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
In your editChartView.mxml add the below code.

Also note this is not complete code you need to display

may be like app.myViewStack.selectedChild=ViewOnly;

The way we are trying to access the viewstack, is we are getting a parentApplication object and type casting to main as its defined in main.mxml and then displaying approriate view.

So you need to invoke this changeMyView() method on some event may as of now to try a simple button click event.
<mx:Script>
	<![CDATA[
 
public function changeMyView():void
{
var app:main = this.parentApplciation as main;
app.myViewStack.  //this is your viewstack now call which ever view you                //want to display.
}
 
	]]>
</mx:Script>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> var app:main = this.parentApplciation as main;

There's a typo in.    ( parentApplciation ==== should be ====> parentApplication )

That's probably why you get the error
>> 1119: Access of possibly undefined property parentApplciation through a reference
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Hmmm thanks for correcting, I generally don;t use flex builder until and unless absolutely needed.
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
Alternative:

The Flex help says:
You can refer to the Application object as mx.core.Application.application from anywhere in the Flex application.

So, in your main add a method:

public function showViewOnly():void {
     myViewStack.selectedChild=ViewOnly;
}

then in your editChartView you can write:

<mx:Button id="searchButton" label="Cancel" click="onSearch()"/>

with

private function onSearch():void {
    mx.core.Application.application.showViewOnly();
}

Author

Commented:
If you don't use Flex Builder "until absolutely needed" what do you use?
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
no no what I meant was i directly use a simple notepad, infact most of the times I type directly in to the textarea provided by EE, not always I have access to Flexbuilder, especially when I am in office I have no access , hence I type code as is, also sorry for the typo. :)

Author

Commented:
That is amazing to me, shivaspk! No problem with the typos. I just appreciate all your help!

Author

Commented:
Thanks again for your assistance!
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> Thanks again for your assistance!
You're welcome.
Thanx 4 axxepting
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.