View Full Version : Attaching custom components with AS3
I woulk like to know if it is possible. Here is an example of component :
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="300">
<mx:Label text="My component" />
</mx:VBox>
Is it possible to call it and attach it from AS3 ? if so, how :confused:
box86rowh
07-01-2008, 09:43 PM
I woulk like to know if it is possible. Here is an example of component :
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="300">
<mx:Label text="My component" />
</mx:VBox>
Is it possible to call it and attach it from AS3 ? if so, how :confused:
Yes it is, just use it as an object.
var test:nameOfComponent = new nameOfComponent();
addChild(test);
thank you :)
Flex is so straight forward that sometimes I can get lost ;)
another problem ccomes along :
I have an xml file like so :
<?xml version="1.0" encoding="UTF-8"?>
<list>
<node label="Test" />
</list>
how can I attach my class called Test.mxml using the label of the node ?
I can extract the label without a problem using:
var item:Object = Tree(pEvt.currentTarget).selectedItem;
but after that, if I do :
var objClass:Class = item.@label as Class;
var test:objClass = new objClass();
styleContainer.addChild(test);
it's not working. What am I missing ?
box86rowh
07-02-2008, 07:50 PM
in your custom component, name the label something like txtLabel then when you create the object:
var test:objClass = new objClass();
test.txtLabel.text = item.@label;
styleContainer.addChild(test);
It's not working, I have a 1180 and a 1046 error on objClass object...
box86rowh
07-02-2008, 08:19 PM
another option is to add a script area in your custom object and put a var in there for the text of the label
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="300">
<mx:Script>
<![CDATA[
[Bindable]
public var theText:String;
]]>
</mx:Script>
<mx:Label text="{theText}" />
</mx:VBox>
Then when you create it do test.theText = item.@label
ok, I probably didn't explain enough.
I am not trying to bind the xml to a custom component.
Inside my XML, I have names of the custom components that I want to attach. So I am trying to convert item.@label into a class or object that I can instantiate and attach to a container.
box86rowh
07-02-2008, 08:32 PM
so you want it to add a label, if the xml is looking for a label?
I want the label 'Test' to be converted to a class, so I can call my class Test.mxml
box86rowh
07-02-2008, 08:40 PM
ah, i was totally on a different track. OK, try this
var classRef:Class = getDefinitionByName(item.@label) as Class;
var myObject:Object = new classRef();
This should take the label "test" and create an object from the test class and name it myObject
it's working but if I attach to the container with an addChild, I have 1118 error. If I change Object to DisplayObject, I have an error too....
box86rowh
07-02-2008, 09:02 PM
when you are doing the addChild cast it as a dispayobject
addChild(myObject as DisplayObject)
yep, not working:
flex throw that error :
ReferenceError: Error #1065: Variable Test is not defined.
at global/flash.utils::getDefinitionByName()
at CSSExplorer/treeClicked()[C:\Documents and Settings\ctuboeuf\workspace\CSSExplorer\src\CSSExp lorer.mxml:61]
at CSSExplorer/__myTree_itemClick()[C:\Documents and Settings\ctuboeuf\workspace\CSSExplorer\src\CSSExp lorer.mxml:82]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:9051]
at mx.controls.listClasses::ListBase/mouseClickHandler()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ controls\listClasses\ListBase.as:9027]
at mx.controls::Tree/mouseClickHandler()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ controls\Tree.as:2922]
box86rowh
07-02-2008, 09:20 PM
is Test your class name? make sure it is not test, also if the custom object is not in the same folder as your mxml you will have include the entire class path in the getDefinitionByName
Test.mxml is the good name.
var classRef:Class = flash.utils.getDefinitionByName("elements."+item.@label) as Class;
var test:Object = new classRef();
container.addChild(test as DisplayObject);
As I am not sure about the path, but it seems to be ok. It still not working....
box86rowh
07-02-2008, 09:33 PM
if you were statically adding this class how would you refer to it in your imports?
you need to be addressing it just like that, remember, it will be case sensitive!
matlevy
07-03-2008, 12:29 AM
1) If Test is not already used in your application it wont get compiled. Try declaring a private variable somewhere in your appliction to ensure the class is included in the compiler.
2) When you pass in the label attribute as the class type in to the getDefinitionByName method you need to include the full class and package name (eg getDefinitionByName('my.package.Test'));
1) If Test is not already used in your application it wont get compiled. Try declaring a private variable somewhere in your appliction to ensure the class is included in the compiler.
That was my problem, thanks. Anyway, I thought this method would be clean, but it's not that clean as I have to declare all my objects (it could be 10 or 100 of them....). Is there a workaround ?
box86rowh
07-03-2008, 03:22 PM
http://www.ultrashock.com/forums/oop/as3-force-class-import-104606.html
They are discussing this issue here and they came up with a few viable options
I came across this thread this morning.
It seems that there is no other solution... I have to force the import.
Thanks a lot for your help :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.