Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Bug 1115773 - Allow L10n's loadJSON to be sync. r=gandalf
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jan 11, 2015
1 parent 7b414ea commit f078581
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions lib/client/l20n/platform/io.js
Expand Up @@ -5,46 +5,33 @@
/* exported io */

var io = {
load: function load(url, callback, sync) {

_load: function(type, url, callback, sync) {
var xhr = new XMLHttpRequest();
var needParse;

if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/plain');
xhr.overrideMimeType(type);
}

xhr.open('GET', url, !sync);

xhr.addEventListener('load', function io_load(e) {
if (e.target.status === 200 || e.target.status === 0) {
callback(null, e.target.responseText);
if (type === 'application/json') {
// Gecko 11.0+ forbids the use of the responseType attribute when
// performing sync requests (NS_ERROR_DOM_INVALID_ACCESS_ERR).
// We'll need to JSON.parse manually.
if (sync) {
needParse = true;
} else {
callback(new L10nError('Not found: ' + url));
xhr.responseType = 'json';
}
});
xhr.addEventListener('error', callback);
xhr.addEventListener('timeout', callback);

// the app: protocol throws on 404, see https://bugzil.la/827243
try {
xhr.send(null);
} catch (e) {
callback(new L10nError('Not found: ' + url));
}
},

loadJSON: function loadJSON(url, callback) {
var xhr = new XMLHttpRequest();

if (xhr.overrideMimeType) {
xhr.overrideMimeType('application/json');
}

xhr.open('GET', url);

xhr.responseType = 'json';
xhr.addEventListener('load', function io_loadjson(e) {
xhr.addEventListener('load', function io_onload(e) {
if (e.target.status === 200 || e.target.status === 0) {
callback(null, e.target.response);
// Sinon.JS's FakeXHR doesn't have the response property
var res = e.target.response || e.target.responseText;
callback(null, needParse ? JSON.parse(res) : res);
} else {
callback(new L10nError('Not found: ' + url));
}
Expand All @@ -58,5 +45,14 @@ var io = {
} catch (e) {
callback(new L10nError('Not found: ' + url));
}
},

load: function(url, callback, sync) {
return io._load('text/plain', url, callback, sync);
},

loadJSON: function(url, callback, sync) {
return io._load('application/json', url, callback, sync);
}

};

0 comments on commit f078581

Please sign in to comment.