2013-06-12 55 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