PDA

View Full Version : get Component from Controller



dedoz
15 Jan 2012, 12:15 AM
Hi, im new to extjs (currently using version 4.1)
My question is : Best way to get reference to a element in my views , from a controller method ?

Like
views/Admin.js
controller/Admin.js

views/Admin.js is using 4 grids, one of them with itemId: 'acrTable'
Inside controller/Admin in a method, i have



myMethod: function()
{
var myGrid = Ext.ComponentQuery.query('#acrTable')[0]
..... do something with myGrid .....
}


Its working fine, but looks weird, i think componentQuery is for looking for many components (its returns a array of elements)

so is there a best way to get a element using its itemId ?

i tried



myMethod: function()
{
var myGrid = Ext.ComponentManager.get('#acrTable');
// says myGrid is null
var myGrid = Ext.ComponentManager.get('acrTable');
// says myGrid is null
}


using Ext.getCmp says the same thing since its a alias of Ext.ComponentManager.get


Should i stick with Ext.ComponentQuery.query()[0] to get components by id ?

sorry for my english.

ot: The view/Admin is actually a panel with 4 items grids, each grid is in one file like view/Grid1.js view/Grid2.js etc. but i didnt want to detail that much to be more clear.

skirtle
15 Jan 2012, 10:57 AM
Component queries are the correct approach, don't go near Ext.getCmp if you can help it.

Have a read of the section titled 'Using refs':

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.app.Controller

I think what I'd do is create a ref to your Admin view (using its xtype) then grab the child components relative to that view when you need them. There are many ways to grab the children, e.g. using a component query or directly by itemId.

You could also consider creating a ref for each child if you feel that's appropriate.

nimaous
20 Jun 2012, 10:47 PM
try this document ..will help you
http://www.sencha.com/learn/the-mvc-application-architecture/ (http://www.sencha.com/learn/the-mvc-application-architecture/)