Last active
December 20, 2015 01:59
-
-
Save StuPig/6052666 to your computer and use it in GitHub Desktop.
use ajax to load script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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