问题
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
回答1:
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'
});
}
})
来源:https://stackoverflow.com/questions/24058929/how-to-do-a-self-destroy-inside-a-custom-component-in-sencha-touch