本文介绍了如何在 Sencha Touch 中的自定义组件中进行自毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

新代码:

Ext.define('Fiddle.MyCmp',{
     extend:'Ext.Component'
    ,alias:'widget.mycmp'
    ,config:{
         html:'MyCmp'
    }
    ,initialize:function() {
        var me = this;
        console.log(me);
        Ext.Function.defer(me.destroy, 5000, me);

        Ext.Function.defer(function(){
            console.log('after 8 seconds');
            console.log(this);

        }, 8000, me);
    }
});

Ext.application({
    name : 'Fiddle',
    ref:{
        cmp: 'mycmp'

    },

    launch : function() {
        Ext.Viewport.add({
            xtype:'mycmp'
        });


    }
});

仍然在 8 秒后,我仍然可以打印出组件.这是控制台日志:

Still after 8 seconds, I still can print out the component. Here is the console log:

类 {onInitializedListeners: Array[0], initialConfig: Object, id:"ext-mycmp-1", getUniqueId: 函数, getId: 函数...} VM1639:39

8 秒后 VM1639:43 类 {onInitializedListeners: Array[0],初始配置:对象,id:ext-mycmp-1",getUniqueId:函数,getId: 函数...}

after 8 seconds VM1639:43 Class {onInitializedListeners: Array[0], initialConfig: Object, id: "ext-mycmp-1", getUniqueId: function, getId: function…}

我正在尝试在自定义组件中添加自毁功能.但它根本不起作用.


I am trying to add a self-destroy function in a custom component.But it simply doesn't work.

这是我的代码:

Ext.define('NoiseComponent', {
    extend: 'Ext.Component',
    xtype: 'noisestation',
    config: {
        name: null,
        updatedTime: new Date(),
        listeners: {
            destroy: function() {

                console.log("do something before destroy()");
                //thisComponent.destroy();
            },
            updatedata: function(thisComponent, newData, eOpts) {
                var startTime = newData[0].get("NoiseTime");
                this.config.updatedTime = new Date();

            },
            initialize: function(thisComponent, eOpts) {
                console.log("initialize component");

                setTimeout(function() {
                    thisComponent.selfDestory(thisComponent);

                }, 5000);
            }
        }
    },

    drawNoise: function() {

        console.log("drawNoise");
    },
    selfDestory: function(thisComponent) {
        console.log("self-destroy");
        thisComponent.destroy(thisComponent);
    }
});

var c = Ext.create("NoiseComponent");

console.log(c);

c.destroy();
//c.fireEvent('destroy');

console.log("after destroyed");
console.log(c);

setTimeout(function() {
    console.log("after 5s");
    console.log(c);
}, 5000);

这是我得到的控制台日志:

Here is the console log I got:

初始化组件 VM1591:44 类 {onInitializedListeners:Array[0], initialConfig: Object, id: "ext-noisestation-1",getUniqueId: 函数,getId: 函数…} VM1591:72 做点什么在销毁()之前 VM1591:65 销毁之后 VM1591:77 类{onInitializedListeners: Array[0], initialConfig: Object, id:"ext-noisestation-1", getUniqueId: 函数, getId: 函数...}VM1591:78 自毁 VM1591:60 在 destroy() 之前做一些事情5 秒后 VM1591:65 VM1591:81 类 {onInitializedListeners: Array[0],初始配置:对象,id:ext-noisestation-1",getUniqueId:函数,getId:函数…} VM1591:82

这是我的煎茶jsfiddlehttps://fiddle.sencha.com/#fiddle/6cl

Here is my sencha jsfiddlehttps://fiddle.sencha.com/#fiddle/6cl

推荐答案

我想你想在组件初始化 5 秒后销毁它.如果是这样,下面的代码会这样做:

I suppose you want to destroy the component 5s after it is initialized. If so, the following code does it:

Ext.define('Fiddle.MyCmp',{
     extend:'Ext.Component'
    ,alias:'widget.mycmp'
    ,config:{
         html:'MyCmp'
    }
    ,destroy:function() {
        console.log('destroy override');
        this.callParent(arguments);
    }
    ,initialize:function() {
        var me = this;
        Ext.Function.defer(function(){
            console.log('Destroying after delay')
            me.destroy();
        }, 5000, me);
    }
});

Ext.application({
    name : 'Fiddle',
    ref:{
        cmp: 'mycmp'

    },

    launch : function() {
        Ext.Viewport.add({
            xtype:'mycmp'
        });
    }
})

这篇关于如何在 Sencha Touch 中的自定义组件中进行自毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 14:04