[英]ExtJS: Ext.Window Prototypal inherited objects cannot be destroy
[ExtJS 3.4.0]我有一個對Ext.Window
具有原型原型的類,如下所示:
function Cls_MyWindow() {
.....
var SaveButton = new Ext.Button({...});
var CancelButton= new Ext.Button({...});
.....
Cls_MyWindow.prototype.width = Ext.getBody().getWidth() - 800;
Cls_MyWindow.prototype.height = Ext.getBody().getHeight() - 350;
Cls_MyWindow.prototype.plain = true;
Cls_MyWindow.prototype.buttons = [SaveButton, CancelButton];
.....
}
Cls_MyWindow.prototype = new Ext.Window;
Cls_MyWindow.prototype.constructor = Ext.Window;
當顯示此窗口時,可以通過按下CancelButton
或Ext.Window
的內置“ x”按鈕將其Ext.Window
。
當我用CancelButton
關閉它時 , SaveButton
和CancelButton
被正常破壞 。 但是,如果使用“ x”按鈕關閉了按鈕 ,則這些按鈕將無法銷毀 ,因此循環將永遠導致我的應用程序崩潰。
經過一番調查,我在ext-all-debug.js中發現了:
Ext.Panel = Ext.extend(Ext.Container, {
.....
if(Ext.isArray(this.buttons)){
while(this.buttons.length) {
Ext.destroy(this.buttons[0]);
}
}
.....
}
調用Ext.destroy
,這是:
Ext.apply(Ext, function(){
.....
destroy : function(){
Ext.each(arguments, function(arg){
if(arg){
if(Ext.isArray(arg)){
this.destroy.apply(this, arg);
}else if(typeof arg.destroy == 'function'){
arg.destroy();
}else if(arg.dom){
arg.remove();
}
}
}, this);
},
.....
}());
似乎是因為this.buttons
從按CancelButton-是Ext.Component,因此它被正常銷毀。 盡管this.buttons
按下“ x”按鈕,這引發了問題 。
this.buttons
通過不同方式銷毀時不是同一對象? 最好給我灑些燈。 先感謝您。
如果我們處於Ext 3.4.0的范圍之內,而不是回到普通的javascript,則說明您沒有正確完成繼承。 繼承已經在Ext中實現,因此您無需深入研究原型,將構造函數創建為父類的實例,等等。
假設您要定義繼承自Ext.Window
MyWindow
類:
Ext.define('MyWindow',{
extend:'Ext.Window'
,method:function(){/* some stuff */}
,closable:false // don't create close box
,closeAction:'hide'
// etc
});
創建實例:
var win = Ext.create('MyWindow', {
width:800
,height:600
});
win.show();
有關更多信息,請參見Ext.define docs。
幾天后,我從Inheritance中找到了一個使用prototype /“ new”的答案。 我在底部的 第一個代碼塊中的代碼是我弄錯的地方。
Cls_MyWindow.prototype = new Ext.Window;
Cls_MyWindow.prototype.constructor = Ext.Window;
“ 新 ”的“新Ext.Window”運營商有我的錯誤。
答:由於使用了new運算符,當我創建一個新的Cls_MyWindow
實例時,總共調用了2個構造函數:其中一個為Cls_MyWindow
,另一個為Ext.Window
。 然后,它們使它們具有“其自己的按鈕原型”,其中SaveButton和CancelButton在Cls_MyWindow
可用作Ext.Button
對象,而在Ext.Window
可用作普通對象。
用CancelButton關閉Cls_MyWindow->可以用Ext.Window.destroy的方式銷毀按鈕。 用“ x”關閉它->按鈕不能被破壞。
解決方案:我將代碼更改為
//Cls_MyWindow.prototype = new Ext.Window; the old one
Cls_MyWindow.prototype = Ext.Window.prototype;
Cls_MyWindow.prototype.constructor = Ext.Window;
一切正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.