PDA

View Full Version : Problem with listeners



BackgroundTeam
13 Apr 2012, 1:52 AM
What I'm trying to do in this piece of code is to put the functions anadir y quitar as listeners of the check and uncheck events of a checkboxfield.

If you see (despite my noobie condition), I'm trying also the pass the label of the checkboxfield itself as a parameter of the function.

Is this possible? What I'm doing wrong?

Thank you so much in advance. I'm only a beginner in Sencha and any help is so appreciated.



// Rest of declaration of view here
{
xtype: 'checkboxfield',
label: 'Noir',
listeners: {
check: anadir(label)
},
{
uncheck: quitar(label)
}
}
}
]
}
]
}
});


anadir: function(label){
console.log("AƱadiendo...");
var index = storemisOpciones.getCount() - 1;
console.log("Indice: " + index);
storemisOpciones.add({option: label});
console.log(label + " se ha marcado");
index = storemisOpciones.getCount() - 1;
console.log("Indice: " + index);
console.log(storemisOpciones.getAt(index));
storemisOpciones.sync();
}


quitar: function(label){
console.log("Quitando...");
var index = storemisOpciones.getCount() - 1;
console.log("Indice: " + index);
var record = storemisOpciones.findRecord('option', label);
storemisOpciones.remove(record);
console.log(label + " se ha quitado");
index = storemisOpciones.getCount() - 1;
console.log("Indice: " + index);
storemisOpciones.sync();
}

tvanzoelen
13 Apr 2012, 2:02 AM
listeners: {
check: anadir
},
{
uncheck: quitar
}
}


No parenthesis or set the whole function there.


check: function(e){
anadir(e);
}

BackgroundTeam
13 Apr 2012, 2:08 AM
Ok, thanks!

But then how I pass the label of the checkboxfield as a parameter of anadir/quitar as shown in the code?

vietits
13 Apr 2012, 2:12 AM
// Rest of declaration of view here
{
xtype: 'checkboxfield',
label: 'Noir',
listeners: {
change: function(field, newValue){
if(newValue){
anadir(field.label);
} else {
quitar(field.label);
}
}
}
}
]
}
]
}
});

BackgroundTeam
13 Apr 2012, 2:20 AM
No, sorry, it doesn't work for me either :-(

tvanzoelen
13 Apr 2012, 2:54 AM
By the way , I doubt if the event checked is in use anymore, better use the event changed and check the params

change( Ext.form.field.Field this, Object newValue, Object oldValue, Object eOpts )

Via the first param you could access the fieldLabel property .



listeners: {
change: {fn: changeFunction}
}





function changeFunction(field, newValue, oldValue, eOps){
var label = field.fieldLabel;

//newValue is true or false
}

BackgroundTeam
16 Apr 2012, 2:02 AM
Actually the listeners check and uncheck work perfectly (if I don't use the event delegation). The main problem in my case is syntax I think.

Mayor_McCheese
16 Apr 2012, 3:06 AM
I always need a this in front of all function references. E.g.




listeners: { check: this.anadir },
{ uncheck: this.quitar }
}

BackgroundTeam
16 Apr 2012, 8:00 AM
YES! That was the issue. Now it works. Thanks a lot.