PDA

View Full Version : is required to controler.init() after getController() ?



mkaw
1 Jun 2012, 10:20 AM
I'm loading controller dynamically using Ext.require. In my button function i have code:

Ext.require(classPath, function() {
var controller = Ext.App.getController(classPath);
if(Ext.App.isControllerExists==false){
controller.init();
}
});
getController create controller instance but don't call init() function, so controller dont listen for events.
If i don't check isControllerExists, and always call controller.init() controller events are fire many times. I mean normally some events in controller call one time. When i click 3 times on button events call not one but 3 times!
My questions is that:
1. Have i to manually call init or is other possibility to create controller?
2. Have i to check isControllerExist(i'm checking length of Ext.App.controllers) when i create controller?
Simple example. I have button, window and controller.
Button:
Ext.require(classPath, function() {
var controller = Ext.App.getController(classPath);
if(Ext.App.isControllerExists==false){
controller.init();
}
Ext.widget('mywindow').show();
})
Window:
Ext.define('MyApp.view.mywindow', { extend: 'Ext.window.Window',
requires: [ 'myapp.view.mypanel' ], alias: 'widget.mywindow', modal: true, height: 600, width: 800, layout: { align: 'stretch', type: 'hbox' }, initComponent: function() { var me = this; Ext.applyIf(me, { items: [{ xtype: 'panel', flex: 1, layout: 'border', border: 0, items: [ {
xtype: 'combobox', fieldLabel: 'customer', store: 'Customer', displayField: 'name', valueField: 'id', editable: false, listeners: { select: { fn: me.onCustomerSelect, scope: me } }},] }], }); me.callParent(arguments); }, onCustomerSelect: function(combo, records, options){ this.fireEvent('customerSelectionChange', records[0].data.id); },
});
Controller:
init: function() {
this.control({ 'mywindow':{ customerSelectionChange: this.onCustomerSelectionChange } }); }, onCustomerSelectionChange: function(customer){ this.getCustomerStore().load(); }
And when i open window again this.getCustomerStore().load() call more then one time.

vietits
1 Jun 2012, 5:23 PM
Each controller must be initialized (by calling its init() method) one time only. Controllers loaded by application have been initialized at the time of application contructing. You have to initialized controllers loaded by yourself.

Your solution is OK. Below is my solution to avoid calling init() method more than one time:


Ext.require(classPath, function() {
var controller = Ext.App.getController(classPath);
controller.init();
controller.init = Ext.emptyFn;
Ext.widget('mywindow').show();
})

mkaw
2 Jun 2012, 4:17 AM
Ok, thank You. It is great idea with fake function. One more time thanks.

muh-die-kuh
2 Jul 2012, 3:09 AM
Shouldnt the Applications getController method call init, whenever a new Controller is created?

I've fixed this for me using


Ext.override(Ext.app.Application, { getController: function(name) {
var controller = this.controllers.get(name);


if (!controller) {
controller = Ext.create(this.getModuleClassName(name, 'controller'), {
application: this,
id: name
});


this.controllers.add(controller);
controller.init(this);
}


return controller;
}
});