I've got this working almost to where I want it. The part I can't get right may be due to the programming so I'd like to get a review.
I'm making JSON
calls to the Twitch API
that sends back a list of people who are streaming video on code camps channel
on the Twitch website.
I have a semi populated array
of users
to start with. From code camps followers URL
, I push
some more users
to the array
and then I begin to build a user object
and set some properties
to it as well as append
them to HTML
. My Object
is being assembled on the fly and not via a constructor
nor Object.create
.
To figure out if these users
are streaming live or offline, I have to make another JSON
call to another URL
which has a stream object
if user
is live else a null
. So I put a for loop
and make a call for each
of those users to see whether they are streaming or not. The problem now is, the returned JSON with the streaming information
and the previous user objects
are in separate scopes
(though I define user
globally
, I can only capture the very last user object that gets built) and I can't tie the streaming property
I'm getting from my second call
to it. I understand the objects are being lost with each iteration
of the for loop
, but I've tried pushing
each user
as its created into an array
and return
the array
but that's not working properly.
Can this work with the way the code is presently written or the fact that I'm making a JSON
call within a JSON
call is bad practice and the fault is in the structure and I should re-write it?
I need somehow to store each of those initial users
that I build the HTML
with and pass them to the program so I can use those guys anywhere outside.
$(function() {
//JSON URLS, VARIABLES AND USER OBJECT
var fccChannelURL = 'https://api.twitch.tv/kraken/users/freecodecamp/follows/channels/';
var twitchChannelURL = 'https://api.twitch.tv/kraken/channels/'; //user details less streaming status
var twitchStreamsURL = 'https://api.twitch.tv/kraken/streams/'; //stream object if user is online else null
// var fccURL = 'https://api.twitch.tv/kraken/streams/freecodecamp?callback=?';
var usersArr = [];
// var streamersArr = [];
var subs = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb"];
var users = {
id: '',
name: '',
logo: '',
url: '',
stream: false
};
//GET FCC FOLLOWERS AND ADD THEM TO SUBS ARRAY
$.getJSON( fccChannelURL, function( data ) {
for ( var i = 0; i < data.follows.length; i++ ) {
//ADD JSON USERS TO SUBS ARRAY
subs.push(data.follows[i].channel.display_name); //adding ~ 8 followers
}
});
//SET TIMEOUT TO ENSURE ARRAY IS POPULATED AFTER FIRST JSON CALL
setTimeout(function(){
for ( var i = 0; i < subs.length; i++ ) {
$.getJSON( twitchChannelURL + subs[i], function( usersJSON ) {
//POPULATE USER OBJECT
users.id = usersJSON._id;
users.name = usersJSON.display_name;
users.logo = usersJSON.logo;
users.url = usersJSON.url;
//HIDE LOADER
$('.loader').hide();
//CREATE HTML FOR USERS AND PREPEND TO RESULTS
$('#result').prepend('<div id="' + users.id + '" class="item"> <a target="_blank" href="' + users.url + '"> <img class="logo" src="'+ users.logo + '"/> <span class="name">' + users.name + '</span> </a> </div>');
}).done( function() {
$.getJSON( twitchStreamsURL + users.name + '?callback=?', function( streamData ){
if ( streamData.stream !== null ) {
console.log(streamData.stream.channel.display_name); // gives the right guys
/*****
HERE I WANT TO ACCESS THE USERS OBJECTS TO SET THEIR STREAM VALUE TO TRUE. * users * is outside this functions scope
********* */
}
});
});
}
}, 3000);
}); //jQuery end
EDIT:
I just ran console.dirxml()
on the html
element I build: #results
and my for-loop
is creating a div
with all the users
inside it on every iteration..looks like it's rendering ok but that may be a memory leak
? and maybe there's another way to make the call outside of the loop
?