views:

38

answers:

2

Hello,

I would like to know if it is possible to instanciate a class by its id .

For example we have a class extending Ext.grid.GridPanel with an id property, is it possible to instanciate the class just knowing his id (which is the component id in this case) ?

A: 

Not without recursively looping over every object hanging off window, looking for id properties and then finding one that matched.

David Dorward
Eh, Ext Js has utilities like `Ext.getCmp` than make this sort of DOM traversing unnecessary.
Jonathan Julian
A: 

What do you mean by instantiate? If its got an id it has already been instantiated.

An id is a property on the object, not the class. Ids are not static, but belong to instances of the class once you've instantiated them. In the constructor you would set the id, which can be used later on to get an instance of the object:

Ext.getCmp('id');

will give you a reference to the ExtJS component if that is what you're after.

If you already know that its an Ext.grid.GridPanel you can instantiate a new one:

var grid1 = new Ext.grid.GridPanel({
    id: 'grid1'
});
Michael Shimmins
no, because if he extends he can set an id in the constructor BEFORE rendering...
Nexum
He can set the id in the constructor before rendering even if he hasn't extended the class. Instantiation and rendering are separate actions. He was asking how to instantiate by an Id which isn't possible, so I extrapolated the most likely actual meaning of his question.
Michael Shimmins