async method queuing

by Eric Hynds

HTML

<div id="foo"></div>

JavaScript

// http://www.dustindiaz.com/async-method-queues/

(function($) {
    $.fetch = function(url) {
        if (!(this instanceof $.fetch)) {
            return new $.fetch(url);
        }

        var q = this.q = $({}),
            self = this;

        $.get(url, function(resp) {
            console.log(resp);
            self.collection = $('<p>The <span>quick</span> brown fox jumped over the lazy dog.</p>');
            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);

var queue = $.fetch('/echo/html/').appendTo('#foo');

queue.css('color', 'green');

queue.find('span').css({
    fontWeight: 'bold',
    color: 'red'
});