2012-02-10 18 views
4

我正在使用ExtJS 4.0.7,是Ext新增的,並且正在使用新的MVC體系結構。我有一個RadioGroup,在更改時我想用它來顯示更多的UI元素。問題是,更改事件爲RadioGroup激發兩次。我假設這是因爲兩個無線電臺都會觸發一個事件,讓它們的值發生變化。ExtJS 4 RadioGroup更改事件兩次觸發

有沒有一種方法可以偵聽RadioGroup或Radios的相同名稱只會觸發一次的更改?在我使用jQuery和Flex的經驗中,我期望RadioGroup只能觸發一次。

這裏是在我看來,RadioGroup中代碼:

items: [{ 
    xtype: 'radiogroup', 
    id: 'WheatChoice', 
    padding: '', 
    layout: { 
     padding: '-3 0 4 0', 
     type: 'hbox' 
    }, 
    fieldLabel: 'Choose Model', 
    labelPad: 5, 
    labelWidth: 80, 
    allowBlank: false, 
    columns: 1, 
    flex: 1, 
    anchor: '60%', 
    items: [ 
     { xtype: 'radiofield', id: 'SpringWheat', padding: '2 10 0 0', fieldLabel: '', 
      boxLabel: 'Spring', name: 'wheat-selection', inputValue: '0', flex: 1 }, 
     { xtype: 'radiofield', id: 'WinterWheat', padding: '2 0 0 0', fieldLabel: '', 
      boxLabel: 'Winter', name: 'wheat-selection', inputValue: '1', checked: true, flex: 1 } 
    ] 
}] 

下面是我的控制器相關代碼:

init: function() { 
    this.control({ 
     '#WheatChoice': { 
      change: this.onWheatChange 
     } 
    }); 
}, 

onWheatChange: function(field, newVal, oldVal) { 
    //this fires twice 
    console.log('wheat change'); 
} 
+0

確認([小提琴](http://jsfiddle.net/molecule/4sTQQ/))。看起來像一個錯誤。 – 2012-02-10 14:57:20

+0

這不是一個解決方法。我只創建了bug的展示。 – 2012-02-10 15:30:03

+0

對不起,我正在談論我發佈的東西。這是一種解決方法:將選擇器更改爲'#WheatChoice radio',並使用事件偵聽器中的邏輯忽略任何不等於true的新值。 – 2012-02-10 15:37:30

回答

5

http://www.sencha.com/forum/showthread.php?132176-radiogroup-change-event-fired-twice/page2

埃文特說,這將是固定的4.1,所以它絕對是一個錯誤。

但是,您可以處理這種情況很容易地:

//borrowing your function 
onWheatChange: function(rg, sel) { 
    var selection = sel['wheat-selection']; 

    if (!(selection instanceof Object)) { 
     alert(selection); 
    } 

} 

VAR的選擇將成爲新的價值。我知道它不能解決兩個事件觸發問題,但希望這有助於!

+0

我接受這一點,因爲這是一個錯誤和解決方法。謝謝。 – 2012-02-13 15:57:45

0

,因爲初始值被設定可能會被解僱,並觸發事件。嘗試添加第三個無線電元素並查看它是否觸發三次。

+0

我用一個包含四個項目的不同無線電組做了它。它發射兩次。一旦告訴我新選擇的無線電數值發生了變化,並且一次告訴我舊無線電數值更改爲checked = false。 – 2012-02-10 15:00:02

4

它觸發兩次的原因是因爲您的收音機組發生了兩個變化。

單選按鈕正在從checked=true更改爲checked=false,然後您新單擊的單選按鈕將從checked=false更改爲checked=true。這應該是一個預期的事情。

如果您想在單選按鈕不再檢查時執行某些操作,該怎麼辦?你會錯過整個活動。真的,你應該只是做一個檢查你的聽衆功能,只聽取具有checked=true的單選按鈕。

+0

這是我也採用過的解決方案,但我不同意想要了解組中的單選按鈕。理論上,RadioGroup應該提供一個價值和一個事件。 – 2012-02-13 15:58:34

0

試試這個:

change : function(thisFormField, newValue, oldValue, eOpts) { 
      if (!Ext.isArray(newValue.myTransitionsRadio)) { 
       // Code goes here. 
      } 

在我的情況在這裏myTransitionsRadio,是給所有單選按鈕的通用名稱。

由於 Nagendra