PDA

View Full Version : Prevent opening same window twice



gunggung
13 Jan 2011, 3:13 PM
Hi folks,

First post (please be gentle!)

I have subclassed the standard Ext Window class to show details of a contact in a system I am developing. Noting fancy - usual stuff:



MyApp.ContactWindow=Ext.extend(Ext.Window, {
initComponent=function() {
<snip>
}
});
An example of creating an instance of a contact Window as follows:



var win=new MyApp.ContactWindow({
contactId: 1234
}).show();
This could be called for example in the row click handler of a search grid.

What I want to do though is to include some code in either the overridden initComponent method (or possibly the constructor method) of MyApp.ContactWindow which checks if I already have a window open for contactId 1234 if not, instantiate a window as normal, but if so simply return the pre existing window.

I have no difficulty setting the id of the new window to something like
contact_window_1234 and I am able to test for the existence of a component with this id in the constructor.

How though would I abandon the object creation and return this existing window?

(I hope this makes sense!)

Nigel
14 Jan 2011, 12:45 AM
I've done something similar in my application, using a desktop-like environment with multiple windows. My advice would be to do the test for the existing window before instantiating the object, then either instantiate or bring-to-top/restore the existing window. Looking ahead, you'd be best off making this a generic method of your application as you're going to need it for just about every window you produce.
I expect there are other ways as well, and I'd be interested as well to see if anyone else has taken a different approach.
Good Luck!

cesarzea
14 Jan 2011, 2:58 AM
I solve it doing that each time i want to show a window module:



if (Ext.myApp.myModule == null) {
Ext.myApp.myModule = new cls_myModule();
}
Ext.myApp.myModule.init(initParameters);

gunggung
14 Jan 2011, 6:53 AM
If I understand you correctly cesarzea, Ext.myApp.myModule is a singleton factory class for generating windows, and will contain a method something along the lines of:



init: function(config) {
if (Ext.getCmp(config.id)) {
return Ext.getCmp(config.id);
} else {
return new MyApp.ContactWindow(config)
}
}


Is that correct?

Foster
14 Jan 2011, 7:46 AM
Hello,

Thats not the way I interpret the code cesarzea posted. I believe it to be:

Ext.myApp.myModule is the actual variable (the window you are trying to create), and this sees if its already created or not:


if (Ext.myApp.myModule == null ) {


if it's not created already, this variable would be null, so it enters the if statement and executes the function cls_myModule, therefore cls_myModule() is the factory function you are mentioning.. I believe it would return the object you are trying to create:


Ext.myApp.myModule = new cls_myModule();


Then at the last part, it's the either been created if it didnt already exist, or it already existed so we skipped over the create and are good to go, so we call init ( set the parameters, make the window visible, etc )


Ext.myApp.myModule.init(initParameters);