如何在Sencha Touch中的自定义组件内进行自毁

时间:2014-06-05 11:24:20

标签: extjs sencha-touch sencha-touch-2

更新

新代码:

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秒后,我仍然可以打印出组件。这是控制台日志:

  

Class {onInitializedListeners:Array [0],initialConfig:Object,id:   “ext-mycmp-1”,getUniqueId:function,getId:function ...} VM1639:39

     

8秒后VM1639:43 Class {onInitializedListeners:Array [0],   initialConfig:Object,id:“ext-mycmp-1”,getUniqueId:function,   getId:function ...}


我正在尝试在自定义组件中添加自毁功能。 但它根本行不通。

这是我的代码:

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);

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

  

初始化组件VM1591:44 Class {onInitializedListeners:   Array [0],initialConfig:Object,id:“ext-noisestation-1”,   getUniqueId:function,getId:function ...} VM1591:72做点什么   在破坏VM1591:77类之后destroy()VM1591:65之前   {onInitializedListeners:Array [0],initialConfig:Object,id:   “ext-noisestation-1”,getUniqueId:function,getId:function ...}   VM1591:78自毁VM1591:60在destroy()之前做一些事情   5秒后VM1591:65 VM1591:81 Class {onInitializedListeners:Array [0],   initialConfig:Object,id:“ext-noisestation-1”,getUniqueId:   function,getId:function ...} VM1591:82

这是我的sencha jsfiddle https://fiddle.sencha.com/#fiddle/6cl

1 个答案:

答案 0 :(得分:0)

我想你想要在初始化之后销毁组件5s。如果是这样,则以下代码执行此操作:

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'            
        });        
    }
})