2013-12-18 32 views
1

我想創建鏈接組合,例如我們有3個組合:第2個取決於第1個選擇,第3個取決於第2個選擇。所有與遠程存儲。ExtJS 4 MVC創建鏈接組合的最佳方法

現在我用的是這樣的:

onFirstComboSelect: function(t, recs) { 
    var _rec = recs[0]; 
    this.getSecondCombo().enable(); 
    var _secondComboStore = this.getSecondCombo().getStore(); 
    _secondComboStore.getProxy().extraParams.firstComboValue = _rec.get('id'); 
    _secondComboStore.load(); 
}, 

這是偵聽選擇事件,這是工作。對於第三個組合監聽器是相似的。但是,當我想用​​一組參數加載這組連接時,會出現一些問題。我必須爲第一個框設置一個值(我使用setValue()方法),爲第二個組合商店設置extraParams,創建ajax請求,等待答案並在回調中爲第三個組合等運行相同的動作。我不確定這是最好的方法。

例子:

if(window.data) { 
     var _data = window.data, 
      _self = this; 

     if(_data.firstComboValue) { 
      // select first combo value 
      this.getFirstCombo().setValue(_data.firstComboValue); 

      // select second combo value 
      var _secondCombo = _self.getSecondCombo(), 
       _secondComboStore = _secondCombo.getStore(); 
      _secondCombo.enable(); 
      _secondComboStore.getProxy().extraParams.firstComboValue = _data.firstComboValue; 
      _secondComboStore.load({ 
       callback: function() { 
        if(_data.secondComboValue) { 
         _secondCombo.setValue(_data.secondComboValue); 

         //select third combo value 
         var _thirdCombo = _self.getThirdCombo(), 
          _thirdComboStore = _thirdCombo.getStore(); 
         _thirdCombo.enable(); 
         _thirdComboStore.getProxy().extraParams = { 
          firstComboValue: _data.firstComboValue, 
          secondComboValue: _data.secondComboValue 
         }; 
         _thirdComboStore.load({ 
          callback: function() { 
           if(_data.thirdComboValue) { 
            _thirdCombo.setValue(_data.thirdComboValue); 
           } 
          } 
         }); 
        } 
       } 
      }); 
     } 
    } 

另外我有一個想法:而不是使用3個不同的商店(與服務器在我的情況下3個不同.PHP處理)用相同的ID /文本模式3個連擊,我可以用firstId/firstText/secondId/secondText/thirdId/thirdText模型製作一個商店,並將它用於所有3個組合。這樣,我將在每個選擇事件中爲所有組合實現數據,但爲此,我有機會輕鬆地在框中安裝一組數據,而無需執行大量嵌套回調。你對此有何看法?

對於我的問題的拼寫和描述的任何錯誤,我很抱歉,如果需要,我會很樂意給出更詳細的解釋。

回答

0

你不需要在你的情況下寫出三個模型或三個處理程序方法。您可以在每個組合框中定義下一個組合框ID,如下所示。


items:[ 
{ 
    xtype:'combo', 
    id:'combo-1', 
    nextComboId:'combo-2', 
}, 
{ 
    xtype:'combo', 
    id:'combo-2', 
    nextComboId:'combo-3' 
}, 
{ 
    xtype:'combo', 
    id:'combo-3' 
}] 

現在你可以使用一次,常見的方法來處理組合-1和combp-2的select事件,併爲你想你可以儘可能多的連擊做相同的。

 
onComboSelect: function(combo, recs) { 
    var nextCombo = combo.nextComboId; 
    nextCombo.getStore().getProxy().extraParams.comboValue = _rec[0].get('id'); 
    nextCombo.getStore().load(); 
} 

+0

那看起來不錯,但如果我需要什麼,同時設定值3的組合,從組合-1和組合-2,extraParams?另外,它是如何幫助我使用一組參數加載這組連接的?我會用例子更新我的問題。 –

+0

好的..請用一個例子更新問題。 –