jquery state widget

by donabrams

HTML

<div id="tester"> </div>

JavaScript

(function($) {
    //Completed
    $.condWait = function(max_attempts, delay, condFunc, condScope, func, scope /*, arguments */ ) {
        var workerUnroller;
        var worker = function(attempts, max_attempts, delay, flagFunc, flagScope, func, scope, args) {
            if (attempts >= max_attempts) {
                return false;
            }
            if (!flagFunc.apply(flagScope)) {
                //increment attempts
                var args2 = [attempts + 1].concat(Array.prototype.slice.call(arguments, 1));
                setTimeout(workerUnroller, delay, args2);
            }
            else {
                //apply just the arguments!
                func.apply(scope, args);
            }
        };
        workerUnroller = function(array) {
            worker.apply(this, array);
        };
        worker(0, max_attempts, delay, condFunc, condScope, func, scope, Array.prototype.slice.call(arguments, 6));
    };
    $.udel = $.udel || {};
    $.udel.template = function(args) {
/*
        args: {
            //any (1) of the following:
            url: null,
            templateString: "",
            template: null
        }*/
        this.ready = false;
        if (args.template) {
            this.template = args.template;
            this.ready = true;
        }
        else if (args.templateString) {
            this.template = $.template(args.templateString);
            this.ready = true;
        }
        else if (args.url) {
            var that = this;
            $.ajax({
                dataType: "text",
                url: args.url,
                type: "GET",
                success: function(tmplString) {
                    that.template = that._build(tmplString);
                    that.ready = true;
                },
                error: function() {
                    that.template = "Error loading template.";
                }
            });
        }
        this.apply = function(data, target) {
            var that =...