Skip to content

Instantly share code, notes, and snippets.

@czbaker
Created June 10, 2015 01:32
Show Gist options
  • Select an option

  • Save czbaker/59ee63616b33fcb3219e to your computer and use it in GitHub Desktop.

Select an option

Save czbaker/59ee63616b33fcb3219e to your computer and use it in GitHub Desktop.
var urlBase = "stuff";
var names = ['blah', 'blah', 'blah', 'blah', 'blah'];
// This will add the URL to your names, making them proper Twitch URLs.
_.map(names, function(name) {
return urlBase + name;
});
// We need an empty array here to hold the data we're getting from Twitch.
var responses = [];
// Now, we iterate over the array (which are now URLs, not names), and do ajax stuff
_.each(names, function(url) {
$.getJSON(url, function(theResponse) {
// This takes our responses array and adds each response to the array as it comes in.
responses.push(theResponse);
});
});
/*
Now, what I would suggest at this point is that you start building DOM elements dynamically instead of doing it how you were.
I would have an empty <div> in you document with an ID that you can use like #twitch or something to base the DOM on.
Both ways would probably work, but I'd imagine this would be better. You can do something like this...
*/
_.each(responses, function(data) {
// Check to see if the stream is online, etc.
if (data.stream) {
var stream = data.stream;
var streamdiv = $("#twitch").append("<div class='twitchuser'></div>");
streamdiv.append("<h2>" + stream.display_name + "</h2>");
} else {
$("#twitch").append("<h2>Stream Offline</h2>")
}
});
// In theory, the above code should dynamically build a div per person, and add a <h2> with the display_name from their JSON object.
// If this shit is broken, I can try to fix it tomorrow, I'm sorta tired, but this should get you started.
// You'll want to use streamdiv.append to create HTML elements and that's sort of how it works. Look at the following:
// http://api.jquery.com/append/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment