?
Solved

calling a function from another mxml

Posted on 2011-05-03
16
Medium Priority
?
875 Views
Last Modified: 2012-08-13
hi guys

i am trying to call a function in one mxml file from another funciton in another mxml file but somehow it doesnt work.
funciton in projectItemrenderer.mxml tries to call function in main.mxml
My code is

main.mxml

<mx:Application....>
<mx:Script>
<![CDATA[
import ria.benziga.controls.onelookClasses.BannerEvent;
public function bannerListner(event:BannerEvent):void {
switch(event.type)
{
case "HomeScreenEvent":                                    
ViewStack.selectedIndex=0;                        
break;                              
case "UserScreenEvent" :                                    
userStack.selectedIndex=1;                        
break;                              
}
}  
]]>
</mx:Script>      
<mx:VBox width="100%" height="100%" borderStyle="solid">
</mx:VBox>
</mx:Application>


my second mxml projectItemrenderer.mxml is in a seperate package

projectItemrenderer.mxml
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="876"  
 cornerRadius="20"
 borderColor="#015793"
 borderStyle="solid"
 borderThickness="2"
 xmlns:projectNav="view.projectNav.*" height="131">
      
<mx:Script>
<![CDATA[
import entity.project.ProjectSummaryDTO;
import mx.controls.Alert;
import mx.core.Application;
            
[Bindable] public var myData:ProjectSummaryDTO;
             
public function gotoAdduser(passProjectId:String):void {      
main.bannerListner();  //i am getting access to main (main.mxml) but i cant access the bannerListner() method in main.mxml.      
}
]]>
</mx:Script>
<mx:VBox width="867"  height="116">
<mx:HBox width="864" horizontalAlign="left" height="49">      
<mx:HBox width="675" horizontalAlign="left" height="44">
<mx:Label id="projectName" text="{myData.name}"  fontSize="17" textDecoration="none" height="36"/>                   
</mx:HBox>
                  
<mx:HBox width="180" horizontalAlign="right" height="34">                   
<mx:LinkButton id="addUser" label="Add User" height="22" width="71" click="gotoAdduser(myData.id)" />
</mx:HBox>
</mx:HBox>      
</mx:VBox>
</mx:HBox>
0
Comment
Question by:royjayd
  • 8
  • 8
16 Comments
 
LVL 29

Accepted Solution

by:
dgofman earned 1600 total points
ID: 35517395
use parentDocument or parent
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35517407
another option to create a static function and call from any mxml without any probelms

and third way create a helper class check cairngorm framework

http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm
0
 

Author Comment

by:royjayd
ID: 35517482
I was actually looking into parsley. what is better parsley / cairngorm  or use nothing ?

thx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:royjayd
ID: 35517499
i have like 25 screens total.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35517711
MVC working for any frameworks :)

I like in cairngorm only View Model implementation, but its inly 3 classes you can copy and paste into your project without dependencies to full framework :)

 
0
 

Author Comment

by:royjayd
ID: 35517801
can you tell me what advantages i have if i use the framework.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35517869
only one advantage if you want to use additional features such as command pattern for webservices (but I hate so much in cairngorm)

Again I like only Model->View->Helper (MVH) implementation from this framework
0
 

Author Comment

by:royjayd
ID: 35689366
Ok I will look into caingorm and maybe open another topic.
Can you plz tell me how I can use parent?
Do I say parent.main.bannerListener() ?

Thx
0
 

Author Comment

by:royjayd
ID: 35690921
parent.main.bannerListener()   doesnt seem to work.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35692561
Try to cast to your application class or parent component

for Example if you have two MXML files

MyApplicationt.mxml
and
MyComponhentView.mxml

From MyComponhentView.mxml you can call your function like this:

MyApplicationt(parentDocument).bannerListener()
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35692578
MyApplication.mxml and MyComponhen.mxml


MyApplication(parentDocument).bannerListener()
0
 

Author Comment

by:royjayd
ID: 35693226
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35693249
Thank you for sharing with me a Question Wizard Screen, but actually I am not planing to submit an question :)

Please can you award points for me.
0
 

Author Comment

by:royjayd
ID: 35694018
looks like i am getting an error. i am using this example
>>>MyApplicationt(parentDocument).bannerListener()

My code is
i have main.mxml and projectItemrenderer.mxml

In main.mxml i have
public function customBannerListner(type:String,projectId:String):void {
switch(type)
{
case "navHomeScreenEvent":                        
mainViewStack.selectedIndex=0;                              
break;                              
case "navUserScreenEvent" :
userView.wizardStack.selectedIndex=0;
break;                              
}
}

In projectItemrenderer.mxml i have

public function gotoAdduser(passProjectId:String):void {      
main(parentDocument).customBannerListner("navUserScreenEvent",passProjectId);
}
...
...
<mx:LinkButton id="addUser" label="Add User" height="22" width="71" click="gotoAdduser(myData.id)" />

but when i click on Add User button i get following error

TypeError: Error #1034: Type Coercion failed: cannot convert view.projectNav::SummaryProjectView@7ee9479 to main.

at projectNav::projectItemrenderer/gotoAdduser()[C:\Documents and Settings\view\projectNav\projectItemrenderer.mxml:32]
at projectNav::projectItemrenderer/__addUser_click()[C:\Documents and Settings\view\projectNav\projectItemrenderer.mxml:53]

SummaryProjectView.mxml contains projectItemrenderer.mxml like this



SummaryProjectView.mxml

<mx:Component id="pjListItemRenderer">
<projectNav:projectItemrenderer myData="{data as ProjectDTO}"> --> projectItemrenderer.mxml
</projectNav:projectItemrenderer>
</mx:Component>      
            
<mx:List id="projectListComponent" width="80%" height="100%" backgroundAlpha="0"
 dataProvider="{projectList}"  itemRenderer="{pjListItemRenderer}" borderStyle="none" visible="{projectList.length > 0}" />
      
</mx:VBox>

0
 
LVL 29

Expert Comment

by:dgofman
ID: 35694574
You parent document is SummaryProjectView

so you can call customBannerListner function if you are implemented

SummaryProjectView(parentDocument).customBannerListner("navUserScreenEvent",passProjectId);
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question