ExtJs 4 MVC get reference of views

In old version of ExtJs how do we get reference of any component? Either we use control.getComponent() method or we use Ext.getCmp(‘controlID’) method. This will give you an instance of particular component. So how do we get instance of views in ExtJs 4 MVC?

it has something called refs. You can add reference to any views in any controller.

refs: [
        {
            ref : ‘myGrid’,
            selector: ‘myGrid’
        }
]

Here selector is the type of the component. This use can set with alias for views. For Example.

Ext.define(‘myApp.myGrid’, {
    extend: ‘Ext.grid.Panel’,
    alias: ‘widget.myGrid’,

    initComponent: function () {   
        this.callParent(arguments);
    }
});

ExtJs MVC has something called component query. This will be used to get reference of particular type of component.

ref  creates a function with name specified there and you can use that function to get reference of that particular component.

You will get reference of myGrid by following code inside controller.

var myGrid = this.getMyGrid();

Remember here that it will not create new instance of grid. It will give a reference to an existing instance. If no instance is available it will return null. To know how to create an instance of view check out my earlier blog here.

This entry was posted in Uncategorized on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

3 thoughts on “ExtJs 4 MVC get reference of views”

  1. Zorglub

    I have trouble with this technic when I've got two instances of same view because this code : var myGrid = this.getMyGrid();
    returns a reference to the first view but never the second.
    Do you have a solution ?

    (Excuse me for my very bad English, I'm French !)

    Reply ↓
  2. vishal

    hi…Zorglub yes u can by this..

    refs: [{
    ref : 'firstGrid',
    selector: 'yourFirstGridParentPanelAlias sameGrid'
    },{
    ref : 'secondGrid',
    selector: 'yourSecondGridParentPanelAlias sameGrid'
    }]

    and now you can get

    var firstGrid = this.getFirstGrid();
    var secondGrid = this.getSecondGrid();

    Reply ↓

Leave a Reply

Your email address will not be published. Required fields are marked *