0

[ExtJS的3.4.0] 我有原型intheritance一类Ext.Window,这样的事情:ExtJS的: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; 

当显示这个窗口,它可以通过按CancelButtonExt.Window的内置“x”按钮关闭。

当我CancelButtonSaveButtonCancelButton关闭它被摧毁正常。 但是,如果是由关闭的“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是不是通过不同的方式销毁?
  • 如果我想/需要保留继承,我有什么解决方案/选项?

甩掉我一些灯是最受赞赏。先谢谢你。

回答

0

经过几天,我发现从Inheritance using prototype/“new”回答。 我的第一个代码块底部的代码是我弄错的地方。

Cls_MyWindow.prototype = new Ext.Window; 
Cls_MyWindow.prototype.constructor = Ext.Window; 

的 “” 在 “新Ext.Window” 运营商有我的错误。

  • 为什么this.buttons是不一样的物体时,它是通过不同的方式来破坏?

答: 由于新的运营商,当我创建Cls_MyWindow共2层构造的新实例获取又叫:Cls_MyWindow之一,另一个Ext.Window。然后使他们拥有“自己的原型按钮”里SaveButtonCancelButtonCls_MyWindowExt.Button对象和Ext.Window作为一个普通的对象

关闭Cls_MyWindow与CancelButton可用 - >按钮可以用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; 

,一切完美的作品

1

如果我们保持在Ext 3.4.0的边界内,不能回到普通的javascript那么你没有正确地继承。继承已经在Ext中实现了,所以你不需要下载原型,创建构造函数作为父类的实例,等等。

让我们假设你要定义MyWindow类从Ext.Window继承:

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文档。

+0

感谢@Saki,虽然它不直接回答我的问题。我已经知道'Ext.define',但在我的项目中Ext.define'奇怪的是** undefined **,我不知道为什么。我不是那种把ExtJS实现到项目中的人,我没有特权去做[这很糟糕]。这就是为什么我使用原型继承。如果你能**给我提供链接**来理解普通的JavaScript prototypal inehritance或**解释**或**点我在哪里我错了**在这,我会接受你的答案。 – Mysteltainn