2011-04-22 18 views

回答

10

如果您命名了匿名函數,它會起作用嗎?另外,您可以從方法調用中獲取對按鈕的引用,因此您不需要使用Ext.getCmp()

Ext.getCmp('myBtn').on('click', function handleClick(button, event){ 
    alert('Alert this message only in first click.'); 
    button.removeListener('click', handleClick); 
}) 
+2

我使用你的解決方案,短期的removeListener - > button.un('click',handleClick); – diewland 2011-04-22 11:17:46

5

嘗試使用命名函數而不是匿名函數。

var myfunction = function() { 

    alert('Alert this message only in first click.'); 
    Ext.getCmp('myBtn').un('click', myfunction); 
} 

Ext.getCmp('myBtn').on('click',myfunction); 
+0

工作過。感謝您的快速響應。 – diewland 2011-04-22 11:16:56

25

如果你想準確地一次觸發事件,使用: -

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的快樂:)

+0

謝謝ExtJS風格:) – diewland 2011-04-26 04:51:31