Skip to content

Instantly share code, notes, and snippets.

@kardeiz
Created June 26, 2012 20:17
Show Gist options
  • Save kardeiz/2998621 to your computer and use it in GitHub Desktop.
Save kardeiz/2998621 to your computer and use it in GitHub Desktop.
jQuery AJAX call to show computer lab availability feed
// Do everything inside AJAX call
$.ajax({
url: "https://restapi.tcu.edu/labs/labs.jsonp",
dataType: "jsonp",
jsonp: false,
// Custom callback required due to hardcoded server callback variable
jsonpCallback: "labsCallback",
success: function(data) {
newarr = [];
// Get data, frame, and push to empty array
$.each(data.ComputerLabs.Lab, function(i,v) {
if (v.LabName == "Library-Windows Desktops-OPEN-Lib IC")
{ newarr.push([0,$('<p/>').text(v.LabsAvailable + " of " + v.LabsTotal + " Windows Desktop computers are available")]) }
if (v.LabName == "Library-Mac Desktops-OPEN-Lib IC")
{ newarr.push([1,$('<p/>').text(v.LabsAvailable + " of " + v.LabsTotal + " Mac Desktop computers are available")]) }
if (v.LabName == "Library-Windows Laptops-OPEN-LIB IC")
{ newarr.push([2,$('<p/>').text(v.LabsAvailable + " of " + v.LabsTotal + " Windows laptop computers are available")]) }
if (v.LabName == "Library-Mac Laptops-OPEN-Lib IC")
{ newarr.push([3,$('<p/>').text(v.LabsAvailable + " of " + v.LabsTotal + " Mac laptop computers are available")]) }
});
// Start building container element. Change this selector
$('#container').html('<h4>Information Commons Lab Status</h4><p>(Information below is updated every five minutes.)</p>');
// Sort and append data to container element
$.each(newarr.sort(function(a,b) {return a[0] - b[0]}), function(i,v) { $('#container').append(v[1]); });
$('#container').append($('<p/>').text('(Note: some laptops may be charging.)'));
}
});​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment