目標是僅在第一次點擊時顯示警報框。在ExtJS中,如何在事件發生時突然解除綁定?
Ext.getCmp('myBtn').on('click', function(){
alert('Alert this message only in first click.');
Ext.getCmp('myBtn').un('click', this); // my attempt, still not work
})
謝謝。
目標是僅在第一次點擊時顯示警報框。在ExtJS中,如何在事件發生時突然解除綁定?
Ext.getCmp('myBtn').on('click', function(){
alert('Alert this message only in first click.');
Ext.getCmp('myBtn').un('click', this); // my attempt, still not work
})
謝謝。
如果您命名了匿名函數,它會起作用嗎?另外,您可以從方法調用中獲取對按鈕的引用,因此您不需要使用Ext.getCmp()
。
Ext.getCmp('myBtn').on('click', function handleClick(button, event){
alert('Alert this message only in first click.');
button.removeListener('click', handleClick);
})
嘗試使用命名函數而不是匿名函數。
var myfunction = function() {
alert('Alert this message only in first click.');
Ext.getCmp('myBtn').un('click', myfunction);
}
Ext.getCmp('myBtn').on('click',myfunction);
工作過。感謝您的快速響應。 – diewland 2011-04-22 11:16:56
如果你想準確地一次觸發事件,使用: -
Ext.getCmp('myBtn').on('click', function(){
alert('Alert this message only in first click.');
}, null, {single: true});
的{single:true}
將要運行這個匿名函數正是出動後一次,所以這應該完成你的要求。
注意:null
實際上是有用的作用域轉換器。您可能需要這種改變在匿名函數的範圍
退房ExtJS的活動@http://docs.sencha.com/extjs/5.0.0/core_concepts/events.html
同時檢查事件選項@http://docs-origin.sencha.com/extjs/5.0.0/apidocs/#!/api/Ext.mixin.Observable-method-on
ExtJS的快樂:)
謝謝ExtJS風格:) – diewland 2011-04-26 04:51:31
我使用你的解決方案,短期的removeListener - > button.un('click',handleClick); – diewland 2011-04-22 11:17:46