PDA

View Full Version : Show a Window once (ignore other tries to show it)



olivierpons
11 Feb 2010, 10:05 AM
Hi!

Suppose I have a class that defines a new Panel.

Suppose I do one Ajax call and it returns an error.
As soon as the error is triggered I show an error Panel (code follow) this way:


new WindowErrorPanel( 'error message' ).show();

Now, suppose I do three Ajax calls and they all return the same error.
As soon as the error is triggered I show an error Panel (code follow) the same way:

new WindowErrorPanel( 'error message' ).show();

This means there will be 3 Panels and the user has to click three times.
I just want to show this error panel once i.e. if it's already showing I do nothing.

NB: I code everything in classes. This implies I do not want to create a "global" WindowErrorPanel and in my classes code I don't want something like:


myGlobalWindowErrorPanel.setMessage( 'error message' );
myGlobalWindowErrorPanel.show();

(Which could be a solution actually but I don't want it, this is not clean code to me).
I want it to be clean and re-usable code.

Any idea/advice how I should do?

Here's my simple WindowPanelError class:


WindowErrorPanel = function(errorString,specialCaption) {
WindowErrorPanel.superclass.constructor.call(this, {
layout:'fit',
width:300,
height:150,
closable: false,
resizable: false,
plain: true,
modal: true,
border: false,
frame:true,
title:'Erreur',
items: [{
html:errorString,
readonly: true,
frame:true,
width:'99%',
height:'99%'
} ],
destroyTask: new Ext.util.DelayedTask(function(){
this.destroy();
},this),
buttons:[{
text:'Ok',
scope: this,
handler:function(btn){
btn.disable();
this.destroyTask.delay(50);
}
}]
});
if(typeof specialCaption=='string') {
this.title=specialCaption;
}
};
Ext.extend(WindowErrorPanel, Ext.Window, {
initComponent: function(){
WindowErrorPanel.superclass.initComponent.call(this);
}
});

Eugen_
11 Feb 2010, 10:26 AM
Hello olivierpons,

Use a WindowMgr (http://www.extjs.com/deploy/dev/docs/source/WindowManager.html#cls-Ext.WindowMgr)

olivierpons
11 Feb 2010, 11:00 AM
Thank you very much indeed! That's a really good starting point, I'm just looking now for the way to get it by "class", not by "id".

Instead of this code (which works):


var e=Ext.WindowMgr.get('id_window_error_panel');
if (typeof(e)=='undefined') {
new WindowLoginPanel().show();
}


I'd like to do something like:


var e=Ext.WindowMgr.get('class_windows_error_panel');
if (typeof(e)=='undefined') {
new WindowLoginPanel().show();
}

And this would be perfect (to me ;) )

Any other good suggestion?

Eugen_
11 Feb 2010, 12:48 PM
Let me think, I try to reply later...
Why you don't want to use a singleton (myGlobalWindowErrorPanel)? Ext.WindowMgr is a singleton too.

olivierpons
12 Feb 2010, 3:23 AM
Hi!

I don't want to use a singleton because I have to declare it as a global variable (= before the onReady function), whereas all the other variables are declared in the onReady function (which is very clean code from my p.o.v).


Ext.onReady(function(){

Ext.QuickTips.init();

var l_tree = new LabyzTree();
var mainPanel = new MainPanel();

..
}I want all the variables to be declared in the onReady function, and do everything else (which has been possible until now) in classes.

A global variable here, this would mean the beginning of the bad code (if one here why not another one there? And so on). It's my own project, I do it on my spare time, I just want it to be perfect, because I have the time to make it perfect (FYI, see why here (http://stackoverflow.com/questions/1685985/is-4-5-years-the-midlife-crisis-for-a-programming-career/) (read the 1st answer it explains why we have projects and why we want them to be clean)).