views:

11

answers:

1

[FLEX 4 / AS3] I load a new component in a as3 file and I want to pass to it some data, knowing only it's a UIComponent.

ex:

// class1
// ...
_UIClass = getDefinitionByName(_basePath + _UIPath + '.' + _UIName) as Class;
_UI = new _UIClass();
// UI is a IVisualElement
// _UIClass is a Class, and in practice is a reference to the component MXML
// here I want to pass data, like this: _UI = new _UIClass(data); 
// or in another method like this: _UI.addData(data);

How do I do this? thanks ;)

A: 

You can use a cast to a class you created that you know has data in a certain place? Or you can use AS3's built in duck-typing. Or create another object that has your data and the UIComponent in it and pass that around. I think you may be wanting to do it wrong :-).

Final answer:
Use a common interface class and have your classes implement it. Just straight up inheritance.

Gary
can you give an example please? im kind of new in this area
Totty
what are you trying to do on a higher level? I think if you described your issue better, I could help you better.
Gary
I have a mxml component, called UI in this case that i want to add to stage from as. the component name is not known, there is another class that manage the name to load the correct UI component(Core.loadUI("login")) then in this class loads the LoginUI, LoginView, LoginData, LoginCalc and LoginObserver. Then I need to make the LoginObserver to listen to events from LoginUI. Then the message is passed to LoginCalc that do some logic, database stuff and save what is done in the LoginData. The LoginUI must have binded data from LoginData. This is just an example, with a simple problem.
Totty
implement a common interface in all the UIs you could be loading, and in there specify your data getters and setters.http://www.rogue-development.com/blog2/2009/07/implementing-interfaces-in-mxml/Type Inheritance is a major thing to know about object-oriented programming, you should learn it :-), followed by encapsulation.
Gary
as3 has multiple interface inheritance, that's what makes it possible.
Gary
but then i cant specify the _UI as an IVisualElement, right? it's not that easy to make them work together.. even if i have a component that implements a new Interface, i can set the getter, but in the Class that manages to load the UI component I can't use that getter.
Totty
yes, you can. you can use the "is" keyword to check if it's IHavingData after you load it, and then cast is using (_UI as IHavingData).data = yourData from the class that loads it. Am I missing something?
Gary
I've done this: "(_UI as IUIDefault).glueMe(_glueMe);"then i got "TypeError: Error #1009: Cannot access a property or method of a null object reference."I've done IUIDefault interface that contains a method "glueMe"then in the mxml i've added a public method named "glueMe" like in the interface. What's missing here?
Totty
you have to do implements="org.whatever.IUIDefault" in your MXML class to actually implement the interface. "as" returns null if the cast is illegal.
Gary
here's the syntax: http://www.rogue-development.com/blog2/2009/07/implementing-interfaces-in-mxml/
Gary
thanks! now it works! ;)
Totty
you can upvote me u know :-).
Gary