如何在Sencha Touch的自定义组件中进行自毁 [英] How to do a self-destroy inside a custom component in Sencha Touch

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

问题描述

更新:



新代码:

  Ext.define ('Fiddle.MyCmp',{
extends:'Ext.Component'
,别名:'widget.mycmp'
,config:{
html:'MyCmp'

,初始化:function(){
var me = this;
console.log(me);
Ext.Function.defer(me.destroy,我的

Ext.Function.defer(function(){
console.log('8秒后');
console.log(this);

},8000,我);
}
});

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

},

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


}
});

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


类{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',{
extends:'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);

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



< blockquote>
初始化组件VM1591:44 Class {onInitializedListeners:
Array [0],initialConfig:Object,id:ext-noisestation-1,
getUniqueId:function,getId :function ...} VM1591:72在destroy()之前做一些
在销毁VM1591之后,VM1591:65 Class
{onInitializedListeners:Array [0],initialConfig:Object,id:
ext -noisestation-1,getUniqueId:function,getId:function ...}
VM1591:78自毁VM1591:60执行destroy()
VM1591:65 5s后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

解决方案

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

  Ext.define('Fiddle.MyCmp',{
extends: 'Ext.Component'
,别名:'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'

},

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


update:

new code:

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


    }
});

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

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

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.

Here is my code:

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:

initialize component VM1591:44 Class {onInitializedListeners: Array[0], initialConfig: Object, id: "ext-noisestation-1", getUniqueId: function, getId: function…} VM1591:72 do something before destroy() VM1591:65 after destroyed VM1591:77 Class {onInitializedListeners: Array[0], initialConfig: Object, id: "ext-noisestation-1", getUniqueId: function, getId: function…} VM1591:78 self-destroy VM1591:60 do something before destroy() VM1591:65 after 5s VM1591:81 Class {onInitializedListeners: Array[0], initialConfig: Object, id: "ext-noisestation-1", getUniqueId: function, getId: function…} VM1591:82

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

解决方案

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的自定义组件中进行自毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆