I understand what's happening in the following block of code, but unsure of how to fix it:
for (var i = 0; i < songs.length; i++){
var listItem = $('<li/>').appendTo(songList);
var song = songs[i];
var link = $('<a/>', {
id: song.id,
href: '#' + song.id,
text: song.name,
contextmenu: function(e){
var contextMenu = new ContextMenu(song);
contextMenu.show(e.pageY, e.pageX);
//Prevent default context menu display.
return false;
}
}).appendTo(listItem);
}
In this scenario, song, when accessed inside of the contextmenu method, will always be the last item iterated over in the foreach loop. I'm wondering how to make it so that there is a 1:1 relationship between links and their defining songs.
I tried assigning the song object as a property of the link object (accessing via this.song), but it comes up as undefined.