View Full Version : Reference in AS to component

02-23-2009, 10:42 PM
I have a mxml that I'm breaking into components where I can. I have this structure:


Other files are there, but these are the ones that matter. When I broke it into pieces, the AS file no longer sees the notice fields that are now in DHSNotice.mxml.

This produces the error:

noticeTextTitle.htmlText = englishNoticeTextTitle;
noticeTextTop.htmlText = englishNoticeTextTop;
noticeTextList.htmlText = englishNoticeTextList;
noticeTextBottom.htmlText = englishNoticeTextBottom;

"1120: Access of undefined property noticeTextTitle." for each of the lines (and the Spanish equivalents).

The component file has this:

<mx:Text id="noticeTextTitle"

Any ideas of how to reference objects in the component file from AS?

02-27-2009, 08:54 PM
OK, I found the solution. I'm putting it here so others can learn something that
apparently was very, very hard.

Refreshing the problem: I had a large mxml file I wanted to break into components per the suggestion at the Adobe tutorial. I figured the heading, a notice area and a footer would be a good first start to separate. Each has a connection to something else. The header and footer have links. The notice area has text that changes from English to Spanish if the user clicks a button in the main (application) routine.

To fix the link in the header and footer, I had to add:


public function linkIDHS(url:String):void
navigateToURL(new URLRequest(url)); <===========


<!-- ***************************************************************** -->
<!-- ** start of the header area ************************************* -->
... <mx:LinkButton id="headerIDHSLinkText"
label="Illinois Department of Human Services"
click="linkIDHS('http://www.dhs.state.il.us')" <========
left="80" top="0"/>

Same "click" and public function on both. This seems pretty standard.

To fix the button in the main application file, I changed the notice mxml to:

[Bindable] public var noticeTextTitle:String; <=====
[Bindable] public var noticeTextTop:String;
[Bindable] public var noticeTextList:String;
[Bindable] public var noticeTextBottom:String;

This surfaces the variables that are set below. Now the script can set them (public) from outside of here.

<!-- ***************************************************************** -->
<!-- ** start of the notices area ************************************ -->
<mx:Text id="noticeCompTextTitle"
htmlText="{noticeTextTitle}" <=====
text="Text Title"
top="0" />

The local public variable, noticeTextTitle, is set in the script to either the Spanish or English variable that holds the title.

And changed the script to:

import DHSComponents.*;

noticeComp.noticeTextTitle = englishNoticeTextTitle.toString();
noticeComp.noticeTextTop = englishNoticeTextTop.toString();
noticeComp.noticeTextList = englishNoticeTextList.toString();
noticeComp.noticeTextBottom = englishNoticeTextBottom.toString();

The application file:

<!-- ***************************************************************** -->
<!-- ** start of the notice area ************************************* -->
<DHSComp:DHSNotice id="noticeComp" <====

Now I'm going to try to explain the above. Please correct any misinformation.

The invoking (line 74) of the component establishes an id of "noticeComp"
that prefixes the public variables set (line 64-67). Those lines have Spanish
equivalents lower in the script. The default, English, is set in the invocation
of DHSNotice (line 75-78).

I hope this will help others working with Flex.