bigbiff | d006b30 | 2015-03-06 18:36:03 -0500 | [diff] [blame] | 1 | module.exports = function JSONLoader(){ |
| 2 | var self = this; |
| 3 | |
| 4 | function receivedResponse(xhr){ |
| 5 | return xhr.status==200 && xhr.readyState==4; |
| 6 | } |
| 7 | |
| 8 | function handleResponse(xhr,callback){ |
| 9 | xhr.onreadystatechange = function(){ |
| 10 | if ( receivedResponse(xhr) ){ |
| 11 | try{ |
| 12 | callback(null,JSON.parse(xhr.responseText) ); |
| 13 | }catch(err){ |
| 14 | callback(err,null); |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | self.load = function(location,callback){ |
| 21 | var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); |
| 22 | xhr.open("GET", location, true); |
| 23 | handleResponse(xhr,callback); |
| 24 | xhr.send(); |
| 25 | }; |
| 26 | }; |