Possible Duplicate:
Event handlers inside a Javascript loop - need a closure?
I have been trying to get this to work for a while and decided to just ask.
I have the following:
function doSomething(var1, var2) {
dojo.xhrGet({
url:"somUrl",
content: {value1:var1,value2:var2},
preventCache: true,
handleAs:"json",
load: function(response){
for(var i in response.myObject) {
var listItem = new dojox.mobile.ListItem({
label: response.myObject[i].name,
rightText: response.myObject[i].value,
clickable: true,
onClick: function() {
customFunction(response.myObject[i]);
this.transitionTo("someScreen");
}
});
myList.addChild(listItem);
}
},
error:function(e){alert(e);}
});
}
doSomething(myVal1, myVal2);
The line that says customFunction(response.myObject[i]);
always returns the last object in the myObject arrray.
Can someone help me with the syntax so I can make this work correctly? I've been reading about js closures and callbacks but I just can't get it to work.
Thanks