The problem you have is that the value of m, by the time the callback is called, it the one of end of loop. A solution is to protect this value by setting it as value of a variable in an immediately called function :
for (var m in ACTIVETICKETS) {
(function(m){
if (ACTIVETICKETS.hasOwnProperty(m))
{
var marker = new L.Marker(new L.LatLng(ACTIVETICKETS[m].location.x, ACTIVETICKETS[m].location.y));
createHtmlForPopUp(m, function(data)
{
console.log(m);
marker.bindPopup( data ); // calling a function with callback
tile_layer.addLayer(marker);
});
}
})(m);
} // for loop ends here
This is because JavaScript doesn't have block scope, and only creates a new variable scope when a function is invoked.
You can use the same technique with a named function instead of an inline one as above:
function makeTicket(m){
if (ACTIVETICKETS.hasOwnProperty(m))
{
var marker = new L.Marker(new L.LatLng(ACTIVETICKETS[m].location.x, ACTIVETICKETS[m].location.y));
createHtmlForPopUp(m, function(data)
{
console.log(m);
marker.bindPopup( data ); // calling a function with callback
tile_layer.addLayer(marker);
});
}
}
Then do this:
for (var m in ACTIVETICKETS) {
makeTicket(m)
} // for loop ends here
And as a side note, there are very compelling reasons to not use a for-in
enumeration on Arrays, but rather to use a typical for
loop, and you don't need the outside if test, so you could remove it and just do
for (var m =0; m<ACTIVETICKETS.length; m++) {
makeTicket(m)
}