Listing 20.03 - A module for loading JS Bin data
JS Bin
(function () {
"use strict";
function loadData (bin, callback) {
var xhr = new XMLHttpRequest();
var url = "http://output.jsbin.com/" + bin + ".json";
xhr.addEventListener("load", function () {
var data = JSON.parse(xhr.responseText);
callback(data);
});
xhr.open("GET", url);
xhr.send();
}
if (window.gpwj === undefined) {
window.gpwj = {};
}
gpwj.data = {
load: loadData
};
})();
gpwj.data.load("qiwizo", console.log);
Listing 20.03 - A module for loading JS Bin data - Tasks 3 to 5
- Declare a new function, outside the IIFE, called numSessions.
- Add statements to the function to display the number of sessions for the loaded user.
- Replace console.log with numSessions as the callback for the load function.
(function () {
"use strict";
function loadData (bin, callback) {
var xhr = new XMLHttpRequest();
var url = "http://output.jsbin.com/" + bin + ".json";
xhr.addEventListener("load", function () {
var data = JSON.parse(xhr.responseText);
callback(data);
});
xhr.open("GET", url);
xhr.send();
}
if (window.gpwj === undefined) {
window.gpwj = {};
}
gpwj.data = {
load: loadData
};
})();
function numSessions (userData) {
var info = userData.name + " has logged ";
info += userData.sessions.length + " sessions.";
console.log(info);
}
gpwj.data.load("qiwizo", numSessions);