Ext-JS 删除组件陷阱

发布于 2024-11-09 11:36:33 字数 1703 浏览 0 评论 0原文

我有一个组件,其作用类似于表格行,称为 FlightLegComponent,如下所示:

[ flight leg component ] [-] [+]

[ flight leg component] [-] [+]

...

当按下 [-] 按钮时,该组件将从父面板中删除。

我已向 [-] 按钮添加了一个侦听器,并在侦听器中调用

this.remove(theFlightLegComponent);

this”作为父组件的位置。

这会引发异常,显然,您无法删除事件处理程序内的组件...删除它的正确方法是什么?延迟后调用方法?

新:

面板的结构如下:

_flightLegRow: function(removable) {

    var flightLegInput = new xx.yy.zz.search.FlightLegInput({
        columnWidth: .8
    });

    var legId = 'flightLeg-' + this.legs++;

    var c = {

        border: 0,

        width: '90%',

        layout: 'column',

        id: legId,

        items: [

            flightLegInput,

            {
                columnWidth: .2,
                margin: 10,
                border: 0,
                layout: {
                    type: 'column'
                },
                items: [{
                    xtype: 'button',
                    text: '-',
                    disabled: !removable,
                    listeners: {
                        click: Ext.Function.bind(function() {

                            //debugger;
                            this.remove(legId, true);
                        }, this)
                    }
                },{
                    xtype: 'button',
                    text: '+',
                    listeners: {
                        click: Ext.Function.bind(function(){
                            this.add(this._flightLegRow(true));
                        }, this)
                    }
                }]
            }
        ]

    };

    return c;

} 

I have a component acts like a table row, called flightLegComponent like so:

[ flight leg component ] [-] [+]

[ flight leg component] [-] [+]

...

when the [-] button is pressed, that component is meant to be removed from the parent panel.

I have added a listener to the [-] button, and in the listener, i call

this.remove(theFlightLegComponent);

where 'this' is the parent component.

This throws an exception, apparently, you can not remove components inside the event handler... What is the proper way to remove it? invoke a method after a delay?

New:

The panels are structured so:

_flightLegRow: function(removable) {

    var flightLegInput = new xx.yy.zz.search.FlightLegInput({
        columnWidth: .8
    });

    var legId = 'flightLeg-' + this.legs++;

    var c = {

        border: 0,

        width: '90%',

        layout: 'column',

        id: legId,

        items: [

            flightLegInput,

            {
                columnWidth: .2,
                margin: 10,
                border: 0,
                layout: {
                    type: 'column'
                },
                items: [{
                    xtype: 'button',
                    text: '-',
                    disabled: !removable,
                    listeners: {
                        click: Ext.Function.bind(function() {

                            //debugger;
                            this.remove(legId, true);
                        }, this)
                    }
                },{
                    xtype: 'button',
                    text: '+',
                    listeners: {
                        click: Ext.Function.bind(function(){
                            this.add(this._flightLegRow(true));
                        }, this)
                    }
                }]
            }
        ]

    };

    return c;

} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云胡 2024-11-16 11:36:33

您可以删除事件处理程序中需要记住传递正确范围的组件。如果您要删除该组件,它可能会调用父级 autoDestroy 配置,这可能会完全删除它并可能导致空指针异常。我猜测按钮处理程序的函数正在按钮的范围内被调用,并且它抛出异常 this.remove 未定义。任何代码或异常消息都有助于查明问题。

new Ext.button.Button({
    handler: function(){this.remove......},
    scope: this
})

You can remove components in event handlers you need to remember to pass the proper scope. If you are removing the component it may be invoking the parents autoDestroy config witch may delete it entirely and can cause null pointer exceptions. I'm guessing the button handler's function is being called in the scope of the button and it's throwing an exception this.remove is undefined. Any code or exception message would be helpful to pinpoint the problem.

new Ext.button.Button({
    handler: function(){this.remove......},
    scope: this
})
极致的悲 2024-11-16 11:36:33

这是您应该用于该按钮的代码:
b.ownerCt 将是theFlightLegComponent,其ownerCt 将是包含theFlightLegComponent 的面板,这样您就可以将其删除。

{
    xtype: 'button',
    text: '-',
    disabled: !removable,
    handler: function(b) {
        b.ownerCt.ownerCt.remove(legId, true);
    }
}

This is the code you shold be using for the button:
b.ownerCt will be theFlightLegComponent and its ownerCt will the panel that contains theFlightLegComponent, in this way you can remove it.

{
    xtype: 'button',
    text: '-',
    disabled: !removable,
    handler: function(b) {
        b.ownerCt.ownerCt.remove(legId, true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文