[英]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.