PDA

View Full Version : Avoid multiple instance of view in MVC



pratik016
6 Dec 2012, 9:54 PM
Hello,

I am facing the problem at MVC architecture. I might be doing something wrong but cannot identify the actual problem.

I am using Ext.create for getting views. But it creates new instance each time when clicking button. So my event will call multiple times. When creating first time view, event calls one time. second time creating same views event call two times.. and it is increasing on each button click event.

Here is my code. please let me know if I am doing anything wrong here...



/*controller from I am getting other controller's view*/
Ext.define('Test.controller.Landing', {
extend: 'Ext.app.Controller',
views: ['Landing'],


init: function (application) {


var me = this;
this.control(
{

'landing button[action="test"]': {
click: function (t, e, eo) {//t => this, e => event, eo => Eoptional
var c = me.getController('Newpage');
c.init();
var instanceWidget = Ext.widget('newpage');
var ws = Ext.getCmp('right_region');


ws.removeAll();
ws.add(instanceWidget);
ws.doLayout();

}
}
});
this.callParent();
}
});




/*Controller and view that are creating multiple events*/
Ext.define('Test.controller.Newpage', {
extend: 'Ext.app.Controller',
views: ['Newpage'],


init: function (application) {

//Ext.create('widget.landing');
this.control(
{
'newpage': {
afterrender: function (t, eOpt) {
alert('New page');
}
},

'newpage button[action="testc"]': {
click: function (t, e, eo) {//t => this, e => event, eo => Eoptional
/*These events call multiple times*/
alert('newpage button');
}
}
});
this.callParent();
}
});


Also I am attaching file for my small code here.

pratik016
7 Dec 2012, 7:07 AM
Anyone knew this MVC issue?
please let me know. if not getting question then also. I really need the solution for this.

flanders
7 Dec 2012, 11:37 AM
Looking at the fact that you are calling the init() of a controller yourselfs I believe that this is the issue here to. Do not call init() yourself, every time init() is executed, your control() call is executed and the new instance starts listening to the events parallel to the already existing instances Of course, they all handle the event after each other, which makes it look like the event is being fired more then once.

In a nutshell: Don't call int() on a controller, the framework takes care of it if you use Ext.app.Controller.getController()

pratik016
10 Dec 2012, 3:57 AM
Thanks for replying.. in MVC we have to call init function at one time only.
It works now.. thank you for your help.