PDA

View Full Version : How to get window from an inside button



duonglt
18 Jan 2011, 5:40 AM
Hi
Recently, I exercise some Ext code. Simply create a window, in the window I add a FormPanel. The form includes two items: an username field and a password field, it also has two button: save and close. In close button handler function, simply close the window. But I want to reuse this handler function in others window so I need the close button navigate to its highest parent window, get this window and let the window call close() function. But I don not know how to get window from the button.
Here my code:



Ext.onReady(function(){
initLogin();
})

function initLogin(){

var fpItems = [
{fieldLabel : 'Username',
allowBlank: false
},
{fieldLabel : 'Password',
allowBlank: false,
inputType : 'password'
}];

Ext.QuickTips.init();
var loginWindow = new Ext.Window({
title:'Login',

width:300,
height:150,
layout: 'fit',
id:'loginWindow'

});

var closeWindow = function(btn){


alert(btn.ownerCt.ownerCt.ownerCt.xtype);
btn.ownerCt.ownerCt.ownerCt.close();

}


var fpButtons = [{text:'Login'},
{
text: 'Close',
handler: closeWindow
}
];
var fpItems = [
{fieldLabel : 'Username',
allowBlank: false
},
{fieldLabel : 'Password',
allowBlank: false,
inputType : 'password'
}];

var loginForm = new Ext.form.FormPanel({
frame : true,
bodyStyle : 'padding: 6px',
labelWidth : 80,
defaultType : 'textfield',
buttonAlign : 'center',
id : 'loginForm',
defaults : {
msgTarget : 'side',
anchor : '-20'
},
items : fpItems,
buttons: fpButtons
});
loginWindow.add(loginForm);
loginWindow.show();

You can see the code make close button work on this situation:
btn.ownerCt.ownerCt.ownerCt.close();
But there are two points here:
First: it may no work in other situations where many containers wrap together.
Second: When I read Ext Api ownerCt attribute would get a Ext.Container object, but there no close function in Ext.Container object, close function belong to window object. But this code still work, make window close so it confused me so much.
In general, I want to get the parent window from a button without consideration of their hierachi.
Very thankful of any solution.

Regards.

18 Jan 2011, 5:48 AM
In your code, try loginForm.close();

18 Jan 2011, 5:48 AM
MOved to Ext js help. please use that forum in the future.