1
votes

I got a little scope related problem with some js code, maybe somebody can explain to me what I'm making wrong:

I'm using extJs and got this snippet:

Ext.onReady(function(){

 // Form for filter selection
 var formFilter = new Ext.FormPanel({
  // ...
        items: [
   cbGroup = new Ext.form.ComboBox({    
                fieldLabel: 'Group',
    store: dsGroups,
    displayField: 'name',
    valueField: 'number',
    emptyText : '- Please choose a group -',
    listeners:{
      'select': function() {
       alert(cbGroup.selectedIndex +' '+this.selectedIndex);
      }
     }
    })
   ]
    }); 
});

The problem: When I access the combobox over 'this' within the listener function, I get the correct result for the selectIndex property. When I access the combobox over it's var name, I allways get the result '-1'. Thank a lot for your help!

2

2 Answers

0
votes

quick and dirty:

Ext.onReady(function(){
    var self = this;
    ...
    alert(cbGroup.selectedIndex +' '+self.selectedIndex);
0
votes

Try to add the listener after setting up the object. So:

Ext.onReady(function(){
 // Group Combobox:
 var cbGroup = {};
 // Form for filter selection
 var formFilter = new Ext.FormPanel({
  // ...
        items: [
   cbGroup = new Ext.form.ComboBox({    
                fieldLabel: 'Group',
    store: dsGroups,
    displayField: 'name',
    valueField: 'number',
    emptyText : '- Please choose a group -',
    })
   ]
    });
 cbGroup.on('select',function() {
       alert(cbGroup.selectedIndex +' '+this.selectedIndex);
      });
});