PDA

View Full Version : Simple Question : how to completely destroy component



lionel8000
23 Mar 2015, 1:30 AM
Here is my code from extjs in action:My Problem is when I click the button the second time ,something goes wrong and the windows does not show.After debug I find that the variable win is not empty even when I press close button in the popup window ... SO why???? .the windows should be destroyed when I press the close button but why not???After find that the win is not empty I add a "destroy" listern ,and use delete to free win...But failed ,anyone know why??? blow is the code Ext.onReady(function() { var win; var newWindow = function(btn) { if (!win) { win = Ext.create('Ext.window.Window', { html : 'My first vanilla Window',id : 'myWin',height : 200,width : 300, constrain : false }); } win.show(); win.addListener("destroy",function(){ console.log(win); delete win ; console.log(win)}); } new Ext.Button({ renderTo : Ext.getBody(), text : 'Open my Window', style : 'margin: 100px', handler : newWindow });});

skirtle
23 Mar 2015, 3:28 PM
Here's your code re-posted with [CODE] tags so we can read it:


Ext.onReady(function() {
var win;

var newWindow = function(btn) {
if (!win) {
win = Ext.create('Ext.window.Window', {
html : 'My first vanilla Window',
id : 'myWin',
height : 200,
width : 300,
constrain : false
});
}

win.show();

win.addListener("destroy", function(){
console.log(win);
delete win;
console.log(win)
});
}

new Ext.Button({
renderTo : Ext.getBody(),
text : 'Open my Window',
style : 'margin: 100px',
handler : newWindow
});
});

There are several problems here.

First up, get rid of the fixed id. Never use them. Ever:


id : 'myWin',

Next, you're misusing the delete operator:


delete win;

That isn't what delete does in JavaScript, you may be confusing it with the delete operator in other languages. In JavaScript it is used to remove an entry from an object, so in this case it won't have any effect. Just set the value to null or undefined. e.g.:


win = null;

However, this whole approach doesn't really make sense. The point of keeping the window around in the variable win is to avoid the overhead of creating it over and over again. If you're going down that route you should use closeAction: 'hide' so that it won't be destroyed in the first place.

Also keep in mind that destroying a component is an ExtJS concept, not a JavaScript concept. The JavaScript engine has no understanding of 'destruction', so any references to your object will remain until they are re-assigned or drift out of scope and become eligible for garbage collection.