Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extjs4 MVC, how get Model or View from a Controller class?

Ext.define('DigitalPaper.controller.Documents', {
 extend: 'Ext.app.Controller',

 views:  ['Documents'],
 stores: ['Documents'],
 models: ['Documents'],

 init: function() {
     console.log('[OK] Init Controller: Documents');
 } 
});

What's the function to get Model or View of this controller? I've tried

  Ext.getModel('Documents');
  this.getModel('Documents');
  this.getModel();
  this.getDocumentsModel();

Any suggests?

like image 277
Pierluigi B Web Developer Avatar asked Jun 06 '12 15:06

Pierluigi B Web Developer


2 Answers

Solution Found:

In the Controller it is possible to use the reference to the View:

refs: [{
    ref: 'viewDocuments', // will be create the method this.getViewDocuments();
    selector: 'Documents'
}],

So you can get the View with this:

this.getViewDocuments();
like image 95
Pierluigi B Web Developer Avatar answered Oct 09 '22 15:10

Pierluigi B Web Developer


Ext controllers are pretty weird, in that there is a single instance of a given controller, no matter how many related view instances you might have. In most MVC or MVP systems there is one controller instance per view instance.

If you plan to use multiple view instances, then you should not keep references to those views in the controller.

You might want to look into Deft's MVC extension for ExtJs that has one controller instance per view instance (plus dependency injection):

http://deftjs.org/

Anyways, controller.getView() returns a reference to the view CLASS, not an object instance. Same with getModel(). getStore() DOES return a store instance.

In your controller, you can do something like this:

this.viewInstance = this.getDocumentsView().create();

I would also recommend naming your model in the singular. It is not a Documents. It is a Document.

like image 20
Neil McGuigan Avatar answered Oct 09 '22 13:10

Neil McGuigan