Skip to content

Instantly share code, notes, and snippets.

@baldwicc
Last active October 23, 2018 06:39
Show Gist options
  • Save baldwicc/6315577 to your computer and use it in GitHub Desktop.
Save baldwicc/6315577 to your computer and use it in GitHub Desktop.
Loads jQuery asynchronously, then executes queue of functions from an array. After queue is flushed, functions .push()'d to the queue will execute immediately.
/**
* qut-jqueryqueue
* @author Christopher Baldwin [https://staff.qut.edu.au/details?id=baldwicc]
* @licence Simplified BSD License
* @source https://gist.github.com/baldwicc/6315577
*/
/*
Exaple of usage:
var _qutjqq = _qutjqq || [];
_qutjqq.push(function() {
console.log($jQ.fn.jquery);
});
*/
var _qutjqq = _qutjqq || [];
(function() {
/**
* configurable options
* @property {array} opts.jqsrc - paths to jquery
*/
var opts = {
jqsources: [
"//code.jquery.com/jquery-1.10.2.min.js", // public cdn
"${resourcePath}/jquery-1.10.2.min.js" // local fallback
]
};
/**
* true once initialized
*/
var initialized = false;
/**
* callback once jquery script is ready
*/
var callback = function() {
// only init once
if (!initialized) {
$jQ = jQuery.noConflict(true);
// create a queuer instance [Source: http://www.dustindiaz.com/async-method-queues]
var queuer = (function() {
function Queue() {
// store your callbacks
this._methods = [];
// keep a reference to your response
this._response = null;
// all queues start off unflushed
this._flushed = false;
}
Queue.prototype = {
// adds callbacks to your queue
add: function(fn) {
// if the queue had been flushed, return immediately
if (this._flushed) {
fn(this._response);
}
// otherwise push it on the queue
else {
this._methods.push(fn);
}
},
flush: function(resp) {
// note: flush only ever happens once
if (this._flushed) {
return;
}
// store your response for subsequent calls after flush()
this._response = resp;
// mark that it's been flushed
this._flushed = true;
// shift 'em out and call 'em back
while (this._methods[0]) {
this._methods.shift()(resp);
}
}
};
return new Queue();
})();
// initialises _qutjqq method array queue
var init_qutjqq = function() {
// add all existing _qutjqq items to our queuer
for (var i = 0; i < _qutjqq.length; i++) {
if (typeof(_qutjqq[i]) === "function") {
queuer.add(_qutjqq[i]);
}
}
// then, empty it..
_qutjqq = [];
// ..and make sure any future pushes are sent to queuer for execution straight away
_qutjqq.push = function(dat_fn) {
if (typeof(dat_fn) === "function") {
queuer.add(dat_fn);
return true;
} else {
return false;
}
};
};
// wait till ready (jQuery's version of 'ready')
$jQ(document).ready(function() {
init_qutjqq();
queuer.flush();
initialized = true;
});
}
};
/**
* loads a script and trigger callback (using Event.observe)
* @param {string} src script url
* @param {function} callback function to fire once loaded
*/
var loadScript = function(src, callback) {
var head = document.getElementsByTagName("head")[0],
script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.defer = true;
script.src = src;
Event.observe(script, "load", function() {
callback();
});
head.appendChild(script);
};
/**
* init
*/
var init = function() {
// parse and load scripts
for (var i = 0; i < opts.jqsources.length; i++) {
loadScript(opts.jqsources[i], callback);
}
};
// FIRE.
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment