2013-06-12 53 views
0

我在JSP中使用Ext JS組合框。第一次加載頁面時,combox框不顯示。 我在我的Firebug控制檯中看到一個錯誤,關聯商店沒有定義。 (TypeError: cpStore is undefined)Ext JS Combobox第一次不加載

但是,我注意到下一個請求正常工作。

請建議我可以做些什麼來避免這個問題。

function displayCPBox(idPrefix){ 

    var cpSelectList = Ext.create ('Ext.data.JsonStore', { 
     storeId: idPrefix+'cpSelectStore', 
     remoteSort: true, 
     autoLoad: true, 
     proxy: { 
      type: 'ajax', 
      url: proyecto + 'extjs/data/cpData.jsp', 
      reader: { 
       type: 'json', 
       root: 'data', 
       idProperty: 'id' 
      } 
     }, 
     fields: [ 
      {name:'id', type: 'float'}, 
      {name:'codigo', type: 'string'} 
     ] 

    }); 

    var multiCombo = Ext.create('Ext.form.field.ComboBox', { 
     fieldLabel: 'CP', 
     renderTo: idPrefix+'cpSelectCombo', 
     id: idPrefix + 'cpSelectComboBox', 
     displayField: 'codigo', 
     valueField : 'id', 
     width: 200, 
     labelWidth: 50, 
     store: cpSelectList, 
     queryMode: 'remote', 
     minChars: 1, 
     cls : 'cp-margin', 
     listeners :{ 
      select: function(combo, records, eOpts){ 
       getZipInfoForCodigoFormFields(records,document.getElementById(idPrefix+'localidad') ,document.getElementById(idPrefix+'provincia'),document.getElementById(idPrefix+'pais'),doc   ument.getElementById(idPrefix+'zona')); 
      } 
     } 
    }); 

} 

代碼加載過程中設置的值:

var cpStore = Ext.data.StoreManager.lookup('<%=idPrefix%>'+'cpSelectStore'); 
cpStore.on('load',function() { 
    <% 
     Long zipCodeForDisplayLong = oportunidad.getId_lib_cp(); 
     long zipCodeForDisplay = (zipCodeForDisplayLong == null) ? -1 : zipCodeForDisplayLong.longValue(); 
    %> 
    var selectedzipCodeValue= <%=zipCodeForDisplay %>; 
    if(selectedzipCodeValue != -1) 
    { 
     var selectedCPVal = cpStore.findRecord("id",selectedzipCodeValue); 
     Ext.getCmp('<%=idPrefix%>'+'cpSelectComboBox').setValue(selectedCPVal.get("id")); 
    } 
}); 

cpStore.load(); 

回答

0

如果您有範圍的問題,我覺得對你不好的兒子......

你應該在範圍作爲參數傳遞到功能

cpStore.on('load', function(cpStore), this { 
    ...cpStore.findRecord() 
}); 

現在cpStore是您的商店參考。


我確實發現你在這個時候爲商店設置你的聽衆有點奇怪。不知道你在哪裏創建商店,但我覺得這可能是更清潔:

var cpStore = Ext.create('Path.to.store.class'); 
cpStore.on({ 
    load: this.onCpStoreLoad, 
    scope: this 
}); 

onCpStoreLoad: function(cpStore) { 

} 
+0

我重寫了我的代碼,設置加載的值,它現在工作正常。非常感謝。 – user2478960