Skip to content

Instantly share code, notes, and snippets.

@marazmiki
Created April 6, 2020 03:22
Show Gist options
  • Save marazmiki/423c39e03e427ceb6d27eb286ee5f83c to your computer and use it in GitHub Desktop.
Save marazmiki/423c39e03e427ceb6d27eb286ee5f83c to your computer and use it in GitHub Desktop.
const Loader = {
require (scripts, callback) {
this.loadCount = 0;
this.callback = callback;
this.totalRequired = scripts.length;
scripts.forEach((src) => {
this.writeScript(src)
});
},
loaded (evt) {
this.loadCount++;
if (this.loadCount === this.totalRequired && typeof this.callback == 'function') {
this.callback.call();
}
},
writeScript (src) {
const s = document.createElement('script');
s.src = src;
s.addEventListener('load', (e) => {
this.loaded(e);
}, false);
document.querySelector('HEAD').appendChild(s);
}
};
Loader.require([
'{% static "admin/js/jquery.min.js" %}',
'{% static "admin/js/jquery.init.js" %}',
], () => {
console.log('ok, loaded jquery from admin');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment