18

Ok so I have a controller with a method in which I want to load a view.

  1. How do I load a view from a controller?
  2. How do I pass some parameters from the controller to the view when I load it?

Any help is much appreciated.

Rob P.
  • 401
  • 1
  • 6
  • 9

5 Answers5

26

To load a view, you can use Ext.widget(). Use Ext.define() to create a view in your view file. I would recommend using the alias property to define an inline xtype for the view.

When you need to load the view, you create an view using Ext.widget() and specify the xtype (alias for your view). Here is an example:

 // define a window
 Ext.define('MyApp.view.user.Add',
    extend: 'Ext.window.Window',
    alias : 'widget.adduser',
    .
    . // Add other properties, custom properties, methods, event handlers etc..
 });

Now, when you want to create an instance in your user controller, you do:

// create an instance
 var view = Ext.widget('adduser'); // refer the below note!

Note: note that there is no 'widget.'! it automatically gets added to the widget name you pass.

Now, taking about passing parameters. Like Ext.create method, you should be able to pass any parameters as:

 // create an instance with params
 var view = Ext.widget('adduser', {title: 'New User title'});

Regarding ref: refs help you in getting references to Views on your page. They do not help in creating an instance or load a view. If you have your view rendered, you can make use of the ref system to get hold of that instance and manipulate the view. You need to make use of the ComponentQuery to get reference of your view.

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
  • That answers how you would create a view from Controller, but how do you load it to current view? I tried this.getViewportContent().insert(Ext.widget('templatecategorycreate')); where getViewportContent() returns where I wanted to add that view to. It doesn't work. – jaycode Jun 30 '11 at 17:25
  • As the answer below shows, refs CAN indeed help you create appropriate instances – Cine Jan 05 '12 at 05:29
  • 1
    @Cine: refs were added in 4.1. At the time of writing of this answer they weren't part of the api. – Varun Achar Oct 12 '12 at 05:46
  • 1
    @sahroozjefri You need to specify what do you think is wrong with this answer or at which point you need more information. The only point 'abdelolakara' is missing (and that because sencha has changed it) is that refs by now are also capable of creating instances. But that is nothing more than a wrapping for that what 'abdelolakara' wrote. And addition you need to present more information on the version where you want the answers for. By now there are huge differences between 4.x and 4.1.x. All in all I don't understand why you offered a bounty here. – sra Jan 30 '13 at 09:38
22

refs can be used to create new instances as well as access existing ones. By adding the option autoCreate: true to your ref, a call to the getter will result in a new instance being created using the ref definition as its config if no existing component matches the selector.

refs: [{
    ref: 'list'
    ,selector: 'myusersgrid#users'
    ,autoCreate: true

    // any additional options get passed as config when an instance needs to be created
    ,xtype: 'myusersgrid'
    ,itemId: 'users'
    ,store: 'Users'
    ,title: 'Users'
},{
    ref: 'otherList'
    ,selector: 'myusersgrid#administrators'
    ,autoCreate: true

    // any additional options get passed as config when an instance needs to be created
    ,xtype: 'myusersgrid'
    ,itemId: 'administrators'
    ,store: 'SpecialUsers'
    ,title: 'Special Users'
}],

Notice the use of the # to additionally match the itemId so I could have refs to multiple instances of the same xtype

There's also a forceCreate: true option which will make the ref's getter always return a new instance, without it autoCreate will create one instance the first time it's retrieved and then keep returning the same one.

The Mighty Chris
  • 1,578
  • 13
  • 17
  • 1
    This is really good. One thing, be sure to add `xtype`, otherwise Extjs won't know what you want to create. I don't see this documented. Is there a worry that being undocumented means it may go away in future releases? – John Gordon Apr 27 '12 at 15:09
  • @JohnGordon no, the xtype option comes from the fact that when you set autoCreate or forceCreate to true, the entire config object of the ref is passed to Ext.widget() as the config object for the component, which in-turn supports xtype for specifying the class of the object to be instantiated.Also it does appear to be documented under Ext.app.Controller#cfg-refs – The Mighty Chris Oct 11 '13 at 19:53
6

If I understand your question I think you want to use refs, take a look at the docs for Ext.app.Controller: http://dev.sencha.com/deploy/ext-4.0.0/docs/api/Ext.app.Controller.html

Basically you create a list of refs using css selectors:

refs: [
    {
        ref: 'list',
        selector: 'grid'
    }
],

Then later in the class you can access this ref by using get, i.e.:

refreshGrid: function() {
    this.getList().store.load();
}

The getList() method is created for you when you create the ref to 'list'.

joekrell
  • 341
  • 2
  • 4
  • no, you are wrong. ref system is only to get a reference to existing instance of view or components. They cannot be used to instantiate a view! – Abdel Raoof Olakara May 05 '11 at 05:41
  • 1
    @AbdelOlakara: Unfortunately you are the one who is wrong. This answer was completely copy-pasted from the documentation as per the link provided above. If you read the docs, you'll see instantiation is optional, but totally supported. (Also check current version of ExtJs even more options for refs behaviour.) – BenSwayne Oct 04 '13 at 18:09
2

I ran into this same problem. I created a method on my abstract base controller to retrieve the view instance and create on if it does not exist.

This will work properly even after the view has been destroyed - a new one will be created.

Ext.define('My.controller.Base', {
    extend: 'Ext.app.Controller',

    //Retrieves an instance of the top-level view
    //If it has not been created yet than one is instantiated
    //Also, overrides the .close() method on the view to 
    //null out the instance reference on the controller (very necessary)
    getViewInstance: function () {
        var self = this;

        if(!this.viewInstance) {
            if(this.views && this.views.length) {

                var view = this.getView(this.views[0]);

                this.viewInstance = view.create();

                this.viewInstance.close = function () {
                    view.prototype.close.apply(this, arguments);
                    self.viewInstance = null;
                };

            } 
        }

        return this.viewInstance;
    }
});

Now all my controllers can easily access their view from w/i controller code w/o any external variables.

John Strickler
  • 25,151
  • 4
  • 52
  • 68
  • What is `this.views`? An array you pre-define? And how come you always retrieve the first view in `var view = this.getView(this.views[0]);`? Otherwise it's a pretty good idea. +1'ed you. – Joseph Victor Zammit Jun 28 '12 at 15:25
  • `this.views` is actually the list you define on the controller itself. `this.views[0]` returns the first view ('string') (this should really be your only view, or your "main" view). In Sencha's [example](http://docs.sencha.com/ext-js/4-0/#!/guide/application_architecture) you can see how they defined an array of views (strings) on their controller. – John Strickler Jun 29 '12 at 11:46
0

Use Ext.create('Proper File Name to be opened',param1 = me);

In the newly created view, use this.param1 to access the parameters.

EG: Ext.create('view.HelloOverlay, param1 = "Hello", param2 = "World"); in the controller of HelloOverlay, using this.param1 will give "Hello" and this.param2 will give "World".

Sometimes the parameters passed will be present in the view so use this.getView().paramName