my question might be a bit confusing, but here is what i would like to do. i have a script:
<script>
...some script here...
var audioPlaylist = new Playlist("2", [
{
name:"Lismore",
mp3:"http://example.com/song.mp3"
},
{
name:"The Separation",
mp3:"http://example.com/song1.mp3"
}
])
</script>
what i would like to do is to generate that script dynamically using $.get.JSON
var audioPlaylist = new Playlist("2", [
$.getJSON('http://www.example.com/users', function(data) {
$.each(data, function(i,item) {
document.write(name: ''item.fname'',)
document.write(mp3: "http://example.com/"''+item.song_id+''".mp3")
}):
});
])
inside the <script>
is this possible? i've tried the script and it fails.
It would be possible to generate code dynamically to do that, but there is no reason to do that.
Just use the map
method to convert the array of data that you get from the AJAX call into the form that you need for the Playlist
object:
$.getJSON('http://www.example.com/users', function(data) {
var items = $.map(data, function(item) {
return { name: item.fname, mp3: "http://example.com/" + item.song_id + ".mp3" };
});
var audioPlaylist = new Playlist("2", items);
});