1. #1
    Sencha User bkraut's Avatar
    Join Date
    Nov 2007
    Location
    Maribor, Slovenia
    Posts
    402
    Vote Rating
    1
    bkraut is on a distinguished road

      0  

    Default Dynamically instantiate a class

    Dynamically instantiate a class


    Hi,

    I use metagrid, where on double click I have to retrieve from server which form to display. The case is that I have to dynamically instantiate a Form from a retrieved string.

    I tried with:

    PHP Code:
    var className 'FormName';
    var 
    form = new className(); 
    but this does not work.
    How can I do it?

  2. #2
    jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,353
    Vote Rating
    79
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      0  

    Default


    try new window[className];


    Code:
    myClass = function() {
        alert('myClass.constructor called!');
    }
    
    var someClassName = 'myClass';
    
    new window[someClassName]()

  3. #3
    Sencha User bkraut's Avatar
    Join Date
    Nov 2007
    Location
    Maribor, Slovenia
    Posts
    402
    Vote Rating
    1
    bkraut is on a distinguished road

      0  

    Default


    Thanks - I tried, but I had no success.

    See the following.

    PHP Code:
                        myClass = function() {
                            
    alert('myClass.constructor called!');
                        }
                        var 
    someClassName 'myClass';
                        new 
    window[someClassName]() // This works
                        
                        
    var win1 = new Ext.ux.PostBookWindow(); // this works !!!
                        
    var clazzName 'Ext.ux.PostBookWindow'// this does not work !!!
                        
    win = new window[clazzName](); 

  4. #4
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,855
    Vote Rating
    610
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    No, because you don't have a class name called 'Ext.ux.PostBookWindow'.

    If you want to do dynamic classes, register xtypes for the class then use Ext.create().
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  5. #5
    Sencha User
    Join Date
    Feb 2009
    Location
    Glasgow, Scotland
    Posts
    295
    Vote Rating
    5
    Stuart-SwarmOnline will become famous soon enough

      0  

    Default


    Have a look at this question on StackOverflow...

    http://stackoverflow.com/questions/1...vascript-extjs

  6. #6
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,502
    Vote Rating
    47
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Quote Originally Posted by bkraut View Post
    Thanks - I tried, but I had no success.

    See the following.

    PHP Code:
                        myClass = function() {
                            
    alert('myClass.constructor called!');
                        }
                        var 
    someClassName 'myClass';
                        new 
    window[someClassName]() // This works
                        
                        
    var win1 = new Ext.ux.PostBookWindow(); // this works !!!
                        
    var clazzName 'Ext.ux.PostBookWindow'// this does not work !!!
                        
    win = new window[clazzName](); 

    Think about it.

    "." is significant in Javascript. The function is not referenced by a property in the window object named "Ext.ux.PostBookWindow"

    You have to access the property window["Ext"], and then subscript that with ["ux"], and subscript the result of that with "PostBookWindow".

    You will have to have a little function which splits the string on "." and walks down the namespace.

  7. #7
    jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,353
    Vote Rating
    79
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      0  

    Default


    I think using an XType is much simpler, as Evan pointed out.

    you could use Ext.ComponentMgr.create({
    yourCustomXType
    });

  8. #8
    Sencha User bkraut's Avatar
    Join Date
    Nov 2007
    Location
    Maribor, Slovenia
    Posts
    402
    Vote Rating
    1
    bkraut is on a distinguished road

      0  

    Default


    Thanks guys. This solves a thing perfectly.

    Ext.ComponentMgr.create({
    yourCustomXType
    });