Load configs and other text via getPackageDirectoryEntry()

Fixes #127
Turns out this isn't actually any faster, but it is cleaner and doesn't
clutter the console. We'll keep it for now.
This commit is contained in:
Thomas Wilburn 2014-01-12 21:09:44 -08:00
parent 6f762a9e59
commit 742f96f644

View file

@ -1,6 +1,7 @@
define(function() {
var cache = {};
var directory = null;
return {
load: function(name, parentRequire, onLoad, config) {
@ -8,12 +9,24 @@ define(function() {
return onLoad(cache[name]);
}
var xhr = new XMLHttpRequest();
xhr.open("GET", name);
xhr.onload = xhr.onerror = function() {
onLoad(xhr.responseText);
}
xhr.send();
var getFile = function() {
directory.getFile(name, {create: false}, function(entry) {
entry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function() {
cache[name] = reader.result;
onLoad(reader.result);
};
reader.readAsText(file);
});
});
};
if (directory) return getFile();
chrome.runtime.getPackageDirectoryEntry(function(dir) {
directory = dir;
getFile();
});
}
};