Get Programming with JavaScript - Listing 20.03

Listing 20.03 - A module for loading JS Bin data

(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);

Further Adventures

Listing 20.03 - A module for loading JS Bin data - Tasks 3 to 5

(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);