PDA

View Full Version : How to find a reference to a dialog with many dialogs on the same page ?



artur
7 Jul 2007, 2:11 PM
I mostly code in C++/C# , not a lot in JavaScript so forgive me if I asked some silly questions.
So I use this function to show dialogs on my page. I pass to it DialogId and DialogContent (DialogContent is a valid HTML string).




var Dialog = function(){

return {

Show : function(DialogId, DialogContent){

if(!DialogId) {

var DialogCreated = Ext.get(DialogId);
if(!DialogCreated) {
var dialogTemplate = new Ext.Template('<div id="{name}" style=" display: "none", position: "absolute"></div>');
var dialogBodyRef = dialogTemplate.append(document.body,{name: DialogId}, true);
Ext.DomHelper.append(dialogBodyRef, {
tag: 'div',
id: "dialogheader",
cls: "x-dlg-hd" },
false);
dialog = new Ext.BasicDialog(dialogBodyRef, {
modal: false,
autoTabs:false,
width:500,
height:300,
shadow:false,
minWidth:300,
minHeight:250,
proxyDrag: true });

dialog.addKeyListener(27, dialog.hide, dialog);
}
else {
dialog = (???);
}

dialog.body.update(DialogContent);
dialog.center();
dialog.show();
}
}
};
}();


When I first call this method with DialogId='Dialog1' everyfing works OK - I get a perfect dialog. Next I call this method with DialogId='Dialog2'. Still OK - I get a second dialog.
But now I call this method with DialogId='Dialog1'. Unfortunately dialog2 is updated, not dialog1. This is because a variable 'dialog' holds a reference to dialog2 which was last created.
Question: How can I find a reference to dialog1?
Thank you for your help.

evant
7 Jul 2007, 3:51 PM
Exactly the same as you would in C# or C++ ;)

The dialog variable only has scope inside the Show method, so as soon as you exit it you have no reference to the dialog, so you'd probably want to change it like:



var Dialog = function(){
var dialogs = {};
return {

Show : function(DialogId, DialogContent){

if(!dialogs[DialogId]) {
var dialogTemplate = new Ext.Template('<div id="{name}" style=" display: "none", position: "absolute"></div>');
var dialogBodyRef = dialogTemplate.append(document.body,{name: DialogId}, true);
Ext.DomHelper.append(dialogBodyRef, {
tag: 'div',
id: "dialogheader",
cls: "x-dlg-hd" },
false);
dialog = new Ext.BasicDialog(dialogBodyRef, {
modal: false,
autoTabs:false,
width:500,
height:300,
shadow:false,
minWidth:300,
minHeight:250,
proxyDrag: true });

dialog.addKeyListener(27, dialog.hide, dialog);
dialogs[dialogId] = dialog;
}
else {
dialog = dialogs[DialogId];
}

dialog.body.update(DialogContent);
dialog.center();
dialog.show();
}
};
}();

artur
8 Jul 2007, 5:14 AM
Thank you.
I didn't think about an array of pointers ...