Reputation: 407
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?
Upvotes: 9
Views: 20270
Reputation: 407
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();
Upvotes: 9
Reputation: 1747
This should work:
Ext.define('DigitalPaper.controller.Documents', {
extend: 'Ext.app.Controller',
views: ['Documents'],
stores: ['Documents'],
models: ['Documents'],
init: function() {
console.log('[OK] Init Controller: Documents');
// get references to view and model classes which can be used to create new instances
console.log('View', this.getDocumentsView());
console.log('Model', this.getDocumentsModel());
// reference the Documents store
console.log('Store', this.getDocumentsStore());
}
});
These methods are created by a method in the Ext controller that creates the getters. http://docs.sencha.com/ext-js/4-0/source/Controller.html#Ext-app-Controller
Here is what that method looks like:
createGetters: function(type, refs) {
type = Ext.String.capitalize(type);
Ext.Array.each(refs, function(ref) {
var fn = 'get',
parts = ref.split('.');
// Handle namespaced class names. E.g. feed.Add becomes getFeedAddView etc.
Ext.Array.each(parts, function(part) {
fn += Ext.String.capitalize(part);
});
fn += type;
if (!this[fn]) {
this[fn] = Ext.Function.pass(this['get' + type], [ref], this);
}
// Execute it right away
this[fn](ref);
},
this);
},
Upvotes: 2
Reputation: 48236
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):
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.
Upvotes: 7
Reputation: 23586
getModel() and getView() do not return the model / views of the controller - they return instances of these in the app (and if none exist, they will be instanced).
You can simply use this to get the view / model names:
this.views[0]
I'm not sure where you are using your gets ( ie, this.getModel('Details') ), but these should correctly return an instance of the model (the constructor is the only place you might have issues referring to these).
Upvotes: 0