0

so, I just beginning flash builder. to the point

my app package structure:

index.mxml (default) <s:application/>
  [main]
    signin.mxml <s:form/>
    dashboard.mxml <s:group/>
      [content]
        home.mxml
        data_a.mxml
        data_b.mxml
        post.mxml
        and-many-more-mxml-file.mxml
        etc.mxml

index.mxml work as (something like) front controller. that will only load main package component (signin,dashboard).

index.mxml fx:script:

import main.signin;
import main.dashboard;

public var m_signin:main.signin = new signin();
public var m_dashboard:main.dashboard = new dashboard();

private function app_ready():void { //creationComplete
    //...service to check session...
    if(is_signed) {
        addElement(m_dashboard);
    } else {
        addElement(m_signin);
    }
}

success.

we assume that user is signed. then from main/dashboard.mxml I want to load package from main.content.* that defined in array.

main/dashboard.mxml fx:script:

protected var dashboard_content:Object = {};
protected var dashboard_content_list:Array = [
    ['home','Home Admin'],
    ['user_a','User Data A'],
    ['user_b','User Data B']
    //many more.
];
for (var i:int = 0; i < dashboard_content_list.length; i++) {
    var content_class:Class = Class(getDefinitionByName('main.content.'+dashboard_content_list[i][0]));
    var content_obj:Object = new content_class();
    dashboard_content[dashboard_content_list[i][0]] = content_obj; //for refference each component
}

each array from dashboard_content_list explain:

0: package suffix that i want to load (ex: `main.content.home`).
1: button label (for each content).

but the loop is still not working, always ReferenceError: Error #1065.

so what I want todo is: load main.content.* package class and save it on Object in main/dashboard.mxml.

some of my refference (but still not working.): http://www.untoldentertainment.com/blog/2008/11/12/as3-tutorials-instantiate-a-class-dynamically/ http://blogs.adobe.com/cantrell/archives/2010/09/loading-classes-dynamically-in-actionscript-3.html http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getQualifiedSuperclassName() Instantiate a class from a string in ActionScript 3 Dynamically Creating Flex Components In ActionScript AS3: Cast Variable as Dynamic Type https://delfeld.wordpress.com/2009/04/21/object_from_class_name/ http://www.mikechambers.com/blog/2006/06/22/actionscript-3-get-a-class-reference-by-class-name/ http://www.rozengain.com/blog/2009/08/21/getdefinitionbyname-referenceerror-and-the-frame-metadata-tag/

respons to @SameerJain

I have try:

import main.content.*;

//inside creationComplete
var c:Class = getDefinitionByName('main.content.home') as Class; // throw: ReferenceError: Error #1065
var o:Object = new c();
trace(o);

then, I try:

var c:Class = getDefinitionByName(getQualifiedClassName('main.content.home')) as Class;
var o:Object = new c();
trace(c); // return [class String]
trace(o); // return '' (empty)
trace(main.content.home); // return [class home] <<< this what I want.


SOLVED

so, @SameerJain's answer guided me to solving my problem.

protected var dashboard_content_list:Array = [
    /*
    index-0: the class inself <<< this solved my problem!
    index-1: the label button
    */
    [main.content.home,'Home Admin'],
    [main.content.user_a,'User Data A'],
    [main.content.user_b,'User Data B']
    //many more.
];


sorry for my bad english :)
thanks.

Community
  • 1
  • 1
anovsiradj
  • 43
  • 3
  • 9
  • What you are trying to do is using a Flex project and try to make it work like a pure actionscript project. This is not gonna work, either make it a actionscript project or take advantage of the Flex SDK instead of trying to go around it. The Flex SDK does manage the creation of class for you, either let the sdk manage the creation of .mxml or if you want to be the one doing that make them pure .as class. – BotMaster Mar 28 '16 at 16:58
  • ummh, okay. any example? – anovsiradj Mar 29 '16 at 01:24

1 Answers1

1

Flex dont compile non used packages from your import statement. So make sure you import all the required packages and set it as a type of any variables so compiler know that we gonna use that package/class somewhere. A working example could be

import main.content.*;

private var dashboardhome:home;
private var dashboarduser_b:user_a;
private var dashboarduser_a:user_a;

for (var i:int = 0; i < dashboard_content_list.length; i++) {
    var content_class:Class = getDefinitionByName('main.content.'+dashboard_content_list[i][0]) as Class;
    var content_obj:Object = new content_class();
    dashboard_content[dashboard_content_list[i][0]] = content_obj; 
}
Sameer Kumar Jain
  • 2,074
  • 1
  • 13
  • 22