Skip to content

Instantly share code, notes, and snippets.

@ehynds
Created April 5, 2011 20:34
Show Gist options
  • Save ehynds/904483 to your computer and use it in GitHub Desktop.
Save ehynds/904483 to your computer and use it in GitHub Desktop.
// inspired by http://www.dustindiaz.com/async-method-queues/
// (don't actually use this gist; there are better ways)
(function($) {
$.fetch = function(url) {
if (!(this instanceof $.fetch)) {
return new $.fetch(url);
}
var q = this.q = $({}),
self = this;
$.get(url, function(resp){
self.collection = resp;
q.dequeue('async');
});
return this;
};
for (var method in $.fn) {
(function(m) {
$.fetch.prototype[m] = function() {
var self = this,
args = arguments;
self.q.queue('async', function(next) {
self.collection = jQuery.fn[m].apply(self.collection, args);
next();
});
return self;
};
})(method)
}
})(jQuery);
// then...
var queue = $.fetch('/some/html/').appendTo('#foo'); // etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment