Skip to content

Instantly share code, notes, and snippets.

@StuPig
Last active December 20, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save StuPig/6052666 to your computer and use it in GitHub Desktop.
Save StuPig/6052666 to your computer and use it in GitHub Desktop.
use ajax to load script
define(['module'], function (module) {
'use strict';
var storage = (function(){
var uid = new Date,
result;
try {
localStorage.setItem(uid, uid);
result = localStorage.getItem(uid) == uid;
localStorage.removeItem(uid);
return result && localStorage;
} catch(e) {}
})();
var cache = {
get: function (url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
},
load: function (name, req, load, config) {
var cached, url = req.toUrl(name);
if (storage) {
cached = storage.getItem(url);
if (cached !== null) {
load.fromText(name, cached);
} else {
cache.get(url, function (content) {
load.fromText(name, content);
req([name], function (content) {
load(content);
});
try {
storage.setItem(url, content);
} catch (e) {}
});
return;
}
}
req([name], function (content) {
load(content);
});
}
};
return cache;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment