Accesing mxml components from AS class (flex 3)

[FONT=Tahoma][SIZE=3]hey everybody,
I have a big deal here, and havent be able to solve it.
Im creating a flex chat, with php sockets and the AS3 xmlSocket class basically.
I have a main class Chat, there I stablish the connection, send and receive the messages. However, I have seen how it is possible to get access to a button or textArea from an AS script wrote in the main mxml file, I mean, something like this works clearly:

[/SIZE][/FONT] ActionScript Code:
[LEFT]<mx:Script><![COLOR=#000000][[/COLOR]CDATA[COLOR=#000000][[/COLOR]
[COLOR=#0000ff]private[/COLOR] [COLOR=#000000]function[/COLOR] setLabelCOLOR=#000000[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR]myButton.[COLOR=#000080]label[/COLOR]==[COLOR=#ff0000]“Click Me”[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
myButton.[COLOR=#000080]label[/COLOR] = [COLOR=#ff0000]“Clicked”[/COLOR];
[COLOR=#000000]}[/COLOR] [COLOR=#0000ff]else[/COLOR] [COLOR=#000000]{[/COLOR]
myButton.[COLOR=#000080]label[/COLOR] = [COLOR=#ff0000]“Click Me”[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]][/COLOR][COLOR=#000000]][/COLOR]></mx:Script>

&lt;mx:[COLOR=#0000ff]Button[/COLOR] id=[COLOR=#ff0000]"myButton"[/COLOR] label=[COLOR=#ff0000]"Click Me"[/COLOR] click=[COLOR=#ff0000]"setLabel();"[/COLOR]/&gt;

[/LEFT]

[FONT=Tahoma][SIZE=3]
[/SIZE][/FONT][LEFT][LEFT][LEFT]However, when I try to access the same way, but now from my class it does not recognize Im referencing a component at mx.core.Application. I have tried to pass the reference from the application to the class (just as you we do in Flash) but it doesnt work. So my problem is **How do I access mxml components in the main application from an ActionScript class **, I get the messages from the socket and I need to write them on a textArea.
This is how I import the class
ActionScript Code:
[LEFT]<mx:Script>
<![COLOR=#000000][[/COLOR]CDATA[COLOR=#000000][[/COLOR]
[COLOR=#0000ff]import[/COLOR] com.[COLOR=#000080]Chat[/COLOR];
[COLOR=#0000ff]public[/COLOR] [COLOR=#000000]function[/COLOR] initAppCOLOR=#000000[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]var[/COLOR] myChat = [COLOR=#000000]new[/COLOR] ChatCOLOR=#000000[/COLOR];
myChat.[COLOR=#0000ff]Connect[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]][/COLOR][COLOR=#000000]][/COLOR]>
</mx:Script>
[/LEFT]

And my classs:…

 ActionScript Code:
 [LEFT]package com [COLOR=#000000]{[/COLOR]

[COLOR=#0000ff]import[/COLOR] flash.[COLOR=#000080]events[/COLOR].*;
[COLOR=#0000ff]import[/COLOR] flash.[COLOR=#000080]net[/COLOR].[COLOR=#0000ff]XMLSocket[/COLOR];

[COLOR=#0000ff]public[/COLOR] [COLOR=#000000]**class**[/COLOR] Chat [COLOR=#000000]{[/COLOR]
    
    
        [COLOR=#808080]*//private var hostName:String = "192.168.0.102";*[/COLOR]
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000]**var**[/COLOR] hostName:[COLOR=#0000ff]String[/COLOR] = [COLOR=#ff0000]"192.168.0.123"[/COLOR];
        [COLOR=#808080]*//private var hostName:String = "192.168.0.106";*[/COLOR]
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000]**var**[/COLOR] port:uint = [COLOR=#000080]9999[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000]**var**[/COLOR] socket:[COLOR=#0000ff]XMLSocket[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000]**var**[/COLOR] usern:[COLOR=#0000ff]String[/COLOR]=[COLOR=#ff0000]""[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000]**var**[/COLOR] head:[COLOR=#0000ff]String[/COLOR],body:[COLOR=#0000ff]String[/COLOR], msg:[COLOR=#0000ff]String[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000]**var**[/COLOR] window;

        
        [COLOR=#0000ff]public[/COLOR] [COLOR=#000000]**function**[/COLOR] Chat[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
            [COLOR=#808080]*//usern = "Xtian";*[/COLOR]
            socket = [COLOR=#000000]**new**[/COLOR] [COLOR=#0000ff]XMLSocket[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            configureListeners[COLOR=#000000]([/COLOR]socket[COLOR=#000000])[/COLOR];
        [COLOR=#000000]}[/COLOR]
        
        [COLOR=#0000ff]public[/COLOR] [COLOR=#000000]**function**[/COLOR] setUsername[COLOR=#000000]([/COLOR]user[COLOR=#000000])[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]
            [COLOR=#0000ff]this[/COLOR].[COLOR=#000080]usern[/COLOR] = user;
        [COLOR=#000000]}[/COLOR]

        [COLOR=#0000ff]public[/COLOR] [COLOR=#000000]**function**[/COLOR] [COLOR=#0000ff]Connect[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]
            socket.[COLOR=#0000ff]connect[/COLOR][COLOR=#000000]([/COLOR]hostName, port[COLOR=#000000])[/COLOR];
            msgArea.[COLOR=#0000ff]text[/COLOR] += [COLOR=#ff0000]"Connecting to server[COLOR=#000099]**

**[/COLOR]"[/COLOR];


[/LEFT]

The problem is right here!
ActionScript Code:
[LEFT]msgArea.[COLOR=#0000ff]text[/COLOR] += [COLOR=#ff0000]“Connecting to server[COLOR=#000099]**
**[/COLOR]”[/COLOR];
[/LEFT]

msgArea is a TextArea on the main Application.

Thanks for any help, I hope everything is clear
::Goto::

[/LEFT]
[/LEFT]
[/LEFT]

[quote=amarghosh;2329024] ActionScript Code:
[LEFT][COLOR=#0000FF]public[/COLOR] [COLOR=#000000]class[/COLOR] Chat
[COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]private[/COLOR] [COLOR=#000000]var[/COLOR] app:MainMXMLFileName;
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000]function[/COLOR] ChatCOLOR=#000000[/COLOR]
[COLOR=#000000]{[/COLOR]
app = application;
[COLOR=#808080]*//now u can call *[/COLOR]
app.[COLOR=#000080]textAreaID[/COLOR].[COLOR=#0000FF]text[/COLOR] = [COLOR=#FF0000]“blah blah foo”[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]

[COLOR=#808080]//from mxml script of main application class;[/COLOR]
[COLOR=#000000]var[/COLOR] chat:Chat = [COLOR=#000000]new[/COLOR] ChatCOLOR=#000000[/COLOR];
[/LEFT]

tell me if it works – am just guessing :)[/quote]

hey,
Well I tried it before, because its what we generally do on flash, however it seems to be a conflict between data types or it just dont get the reference to the main application that easy. But thank you anyway :slight_smile:

[quote=amarghosh;2329460]change constructor argument type to Object and give a trace in Chat constructor to see what it really receives;

another work around i think will work is:
Try passing this.parentApplication instead of this. Chat constructor should listen for Object rather than any particular type; then give a trace in constructor to find what was really the data type of it;

ActionScript Code:
[LEFT][COLOR=#0000FF]public[/COLOR] [COLOR=#000000]**function**[/COLOR] Chat[COLOR=#000000]([/COLOR]app:[COLOR=#0000FF]Object[/COLOR][COLOR=#000000])[/COLOR]

[COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];[COLOR=#808080]//[Object ?][/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#808080]//call it from mxml[/COLOR]
[COLOR=#000000]var[/COLOR] chat:Chat = [COLOR=#000000]new[/COLOR] ChatCOLOR=#000000[/COLOR];
[/LEFT]

[/quote]

Got it man! Thank you…
pass ‘this’ works, I think the datatype was the problem, or I was just making a big mistake
Anyway thank you, Im still a little silly in flex

::Goto::