PDA

View Full Version : Passing a local variable into an event handler



Ppecher
23 Feb 2011, 9:40 AM
h.d.d.w = Ext.extend(w.g.a,
{
initComponent: function () {
exampleFunctionA('Monday');
//other fields omitted
}
exampleFunctionA: function (dayOfWeek) {
this['WeekdayHoursStore' +dayOfWeek] = new Ext.data.Store({
proxy: a.b.c.getProxy('d/e.asmx/f'),
reader: g.h.i.j(
[
//array of config objects removed
]),
sortInfo: //omitted
});

this['WeekdayHoursStore' +dayOfWeek].load(
{
params:
{
//parameters removed
}
});
this['WeekdayHoursStore' +dayOfWeek].on("load", this._renderHours, this);
//irrelevant code removed
},

_renderHours: function (dayOfWeek) {
var dayIndex;
for(var i = 0; i<7; i++){
if(this.weekdays[i] === dayOfWeek){
dayIndex = i;
break;
}
}
var record = this.WeekdayHoursStore.getAt(dayIndex);
this['UseDefaultValue' +dayIndex] = record.get("UseDefault");
}
//further class members omitted
}
How can I pass dayOfWeek into _renderHours?

drian
23 Feb 2011, 9:53 AM
Edited: nvm, my idea was wrong

johnathanhebert
23 Feb 2011, 11:09 AM
Call createDelegate on the argument that references _renderHours and tell it to make dayOfWeek the first argument by giving it a position of 0:




h.d.d.w = Ext.extend(w.g.a,
{
initComponent: function () {
exampleFunctionA('Monday');
//other fields omitted
}
exampleFunctionA: function (dayOfWeek) {
this['WeekdayHoursStore' +dayOfWeek] = new Ext.data.Store({
proxy: a.b.c.getProxy('d/e.asmx/f'),
reader: g.h.i.j(
[
//array of config objects removed
]),
sortInfo: //omitted
});

this['WeekdayHoursStore' +dayOfWeek].load(
{
params:
{
//parameters removed
}
});
this['WeekdayHoursStore' +dayOfWeek].on("load", this._renderHours.createDelegate(this, [dayOfWeek], 0), this);
//irrelevant code removed
},

_renderHours: function (dayOfWeek) {
var dayIndex;
for(var i = 0; i<7; i++){
if(this.weekdays[i] === dayOfWeek){
dayIndex = i;
break;
}
}
var record = this.WeekdayHoursStore.getAt(dayIndex);
this['UseDefaultValue' +dayIndex] = record.get("UseDefault");
}
//further class members omitted
}
How can I pass dayOfWeek into _renderHours?

Ppecher
23 Feb 2011, 11:49 AM
Thanks.