wait = function(first) {
return new wait.Init(first);
}
wait.Init = function(first) {
var self = this;
this.callbackCaller = function() {
self.callback.apply(self, arguments);
}
first.call(this, this.callbackCaller);
}
wait.Init.prototype = {
deferred: [],
callback: function() {
this.args = [].slice.call(arguments);
this.args.unshift(this.callbackCaller);
if (this.deferred.length) {
this.done = false;
this.deferred[0].apply(this, this.args);
this.deferred.shift();
}
if (!this.deferred.length) {
this.done = true;
}
},
wait: function(run) {
if (this.done) {
this.done = false;
run.apply(this, this.args);
} else {
this.deferred.push(run);
}
return this;
},
data: function(item, value) {
if (arguments.length === 2) {
this.data[item] = value;
} else {
return this.data[item];
}
}
}
Так и не придумал, как коллбек всунуть в прототип: http://jsfiddle.net/finom/XSGub/64/