Ah, that is the problem.  Packages and namespaces are really
compile-time things.  You don't use them in run-time references.
Instead, you want to use the run-time DOM.

 

There are several ways to reach "up" the dom.  One is
application.application.  You use the id of your instantiated component
to reference its public members.  So one way to do what you want is::

Import mx.core.Application;

...

private var _app:Application

...

_app = Application.application;

user = _app.loginpanel1.user

 

below are some additional notes on traversing the DOM.

Tracy

 

Communicating between Components:

 

Note: for "loose coupling" use events.  But that is another topic.

 

A non-trivial flex application is "component" based. While all of the
built in controls are components, the question of communicating between
components really only arises when you are using a custom component. A
custom component, whether implemented in mxml or ActionScript, has its
own "scope". Within that component (Application is a component too!),
all sibling child controls share the same scope, so you can refer to
controls by their id.  If the controls(components) have public
properties or methods, you can refernce those members directly through
the id:

            <mx:TextInput id="textinput1" text="test value" .../>

            <mx:Text id="text1" ... text="{textinput1.text}" .../>

 

Ok, so far, its a "duh" right?

 

When you use custom components in a Flex app, at run time they make a
document object model hierarchy (DOM).  Each subcomponent has its own
scope and can't *directly* reference the member properties or methods of
its sibling subcomponents.

 

So again, within a component, you can refernce siblings directly, as in
the example above.  But there are two other cases inherent in a
hierarchy.  You might want to reference "up", to get to public members
of the parent, or 'down", to get to public members of some child.

 

Accessing members in the parent:

On an ordinary component DOM, you can reference the parent component
using the .parent property.  Say that a control with id="textinput1"
exists in the parent of the current component.  then you could do:

            <mx:Text id="text1" ... text="{parent.textinput1.text}"
.../>

 

Accessing members in the main application:

Components can be nested, sometimes very deeply.  If the reference you
want is all the way at the top-level, main application (the one with the
<mx:Application> tag), you could do
{parent.parent.parent.textinput1.text}, but you would have to count the
component levels just right.  Instead, you can use
Application.application to get to that scope:

            <mx:Text id="text1" ...
text="{Application.application.textinput1.text}" .../>

You can shoretn this style of reference by importing
mx.core.Application, and assigning Application.application to a
variable, like _app, the doing (_app.textinput1.text)

 

Accessing members of a child component:

Say that in this case, a child component has the TextInput control you
want to reference.  First, make sure the child component has an id:

            <myComp:MyCustomComp id="mycc1" .../>

Then, in the same scope (the same component/file that contains as the
tag above) you can say:

            <mx:Text id="text1" ... text="{mycc1.textinput1.text}" .../>

 

Accessing a nested component:

As mentioned above you can go "up" the hierarchy using
"parent.parent...".  You can als go "down" the hirearchy using id
references:

            <mx:Text id="text1" ...
text="{mycc1.mycc11.mycc.12.textinput1.text}" .../>

 

Additional notes:

If you are using SWFLoader to load an entire Application, you can
reference the immediate parent application using "parentDocument".  You
can also use Application.application to reach the main app, as shown
above.

 

Accessing members of an application loaded by SWFLoader is a bit more
complicated.  See the example here:

http://www.cflex.net/showFileDetails.cfm?ObjectID=690

 

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of elr
Sent: Monday, November 12, 2007 11:47 PM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders] reference a class instance from another mxml

 


Yes Tracy  both of my 2 mxml (A and B) are in a  directory   /comps and
both have this:

xmlns:comps="comps.*"  

namespace reference. Maybe I missed something but it sounds correct as
Flex code hinting lists me all my components as soon as I type down
'comps'.

LoginPanel was instanciated in a state of Main.mxml (the Application) 

Here's Main.mxml:

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";  
                  xmlns:user="classes.*" 
                  xmlns:comps="comps.*" >

                  <mx:states>
                          <mx:State name="connected">
                                    <mx:RemoveChild
target="{loginpanel1}"/>
                                   <mx:AddChild position="lastChild">
                                             <comps:UserPanel >
                                             </comps:UserPanel>
                                    </mx:AddChild>
                           </mx:State>
                  </mx:states>
        
                  <comps:LoginPanel id="loginpanel1">
                  </comps:LoginPanel>

</mx:Application>



Here's what my mxml B (UserPanel)  looks like ( User class is located in
/classes dir)

<?xml version="1.0" encoding="utf-8"?>  
<mx:Panel xmlns:         mx="http://www.adobe.com/2006/mxml";  
                           xmlns:user="classes.*" 
                           xmlns:comps="comps.*" 
                           creationComplete="init()">

                  <mx:Script>
                 <![CDATA[
                          import classes.*;
                          private var user:User;

                          private function init():void{
                                   ...
                                    user=comps.LoginPanel.user
//error 1119 at this line !!!
                                   ...      
                          }        
                          ...      
                 ]]>
         </mx:Script>
        ...
</mx:Panel>



Again ,thank's all for your help ;)

Eric 




At 2007-11-12   20:18, you wrote:

        Hmm, "namespace" in this context makes me nervous.  You want to
access subcomponents via their "id", when that id is in scope.  You do
not use namespaces, unless I am misunderstanding your use of the word.
        
         
        
        Show us a bit of code.  How are you instanitating "LoginPanel"?
        
        Tracy
        
         

        
________________________________


        From: flexcoders@yahoogroups.com [
mailto:flexcoders@yahoogroups.com <mailto:flexcoders@yahoogroups.com> ]
On Behalf Of elr
        Sent: Monday, November 12, 2007 6:27 PM
        To: flexcoders@yahoogroups.com
        Subject: RE: [flexcoders] reference a class instance from
another mxml
        
         
        
        No Gordon. unfortunately my instance was public. I even defined
a getter but no way:I still get an 1119 error in my second mxml (B) at
line:
        
        user=comps.LoginPanel.user;
                
        
        where of course comps is the namespace targeting my
LoginPanel.mxml
        
        
        
        Eric
        
        At 2007-11-12   17:05, you wrote:
        
        

        
        When your login component created a User instance, did it store
it in a public instance variable (say, 'user') of the login component?
If so, it should be accessible as myLoginComponent.user.
         
        - Gordon

        
________________________________


        From: flexcoders@yahoogroups.com [
mailto:flexcoders@yahoogroups.com <mailto:flexcoders@yahoogroups.com> ]
On Behalf Of elr
        Sent: Sunday, November 11, 2007 5:03 PM
        To: flexcoders@yahoogroups.com
        Subject: [flexcoders] reference a class instance from another
mxml
        
        
        HI,
        
        I can't find a way to access from an mxml B to a class C that
was 
        instanciated in a mxml A
        
        In my sample. a login component (mxml A) instanciates a
user:User
        As soon as login is done, the second component (mxml B) should 
        retrieve the user instance
        
        I referenced this user instance starting from component that
composes 
        it (mxml A) but I always get a 1119 error:Access of possibly 
        undefined property user through a reference with static Class...
        
        Thx for your help
        
        Eric

         

 

Reply via email to