Permalink
Browse files

jquery

  • Loading branch information...
1 parent 422e12f commit b88ed12e06b03a26b5561b4e44439e4c73ff9779 @cho45 committed Mar 8, 2012
Showing with 247 additions and 117 deletions.
  1. +4 −0 ChangeLog
  2. +0 −23 README.markdown
  3. +5 −1 Rakefile
  4. +55 −20 binding/jquery.js
  5. +19 −7 jsdeferred.jetpack.js
  6. +66 −27 jsdeferred.jquery.js
  7. +3 −3 jsdeferred.js
  8. +19 −7 jsdeferred.nodoc.js
  9. +19 −7 jsdeferred.userscript.js
  10. +10 −10 package.json
  11. +5 −4 package.json.tmpl
  12. +40 −7 test-jsdeferred.js
  13. +1 −0 test-node.js
  14. +1 −1 test.html
View
@@ -1,3 +1,7 @@
+2012-03-08 SATOH Hiroh <cho45@lowreal.net>
+
+ * Fix parallel function.
+
2011-04-19 SATOH Hiroh <cho45@lowreal.net>
* Fix infinate build on Titanium 1.6.1 (by Seasons7)
View
@@ -219,29 +219,6 @@ JSDeferred is inspired by MochiKit Deferred object, so both is similar but JSDef
* MochiKit Deferred has chain by Array. JSDeferred has chain by chain of Deferred.
* MochiKit Deferred separates parent chain and child chain, JSDeferred not.
-## Comparison of other deferred-like objects ##
-
-### vs [jQuery's Deferred]( http://api.jquery.com/category/deferred-object/ )
-
- * Bad: Not supports child deferred
- * Bad: No utility functions
- * Good: Simple
- * Good: Cache result (it can be called after completed the task)
- * Good: jQuery embed (no more dependencies and compact)
-
-### vs [Dojo's Deferred]( http://docs.dojocampus.org/dojo/Deferred )
-
- * Bad: Not supports child deferred
- * Bad: Very heavy dependencies for dojo
- * Good: Support progress
-
-### vs [CommonJS's Promise]( http://wiki.commonjs.org/wiki/Promises )
-
- * Bad: Not implemeneted?
- * Bad: Not supports child deferred
- * Bad: Too complex
- * Good: Highlier functional
-
## Different-origin Deferred instances
JSDeferred can be used in inter-environment which is independent respectively like browser extension
View
@@ -190,6 +190,10 @@ file "jsdeferred.jetpack.js" => ["jsdeferred.js", "binding/jetpack.js"] do |t|
end
file "doc/index.html" => ["jsdeferred.js", "doc/template/class.tmpl", "doc/template/publish.js"] do |t|
- sh %{java -jar #{JSDOC}/jsrun.jar #{JSDOC}/app/run.js -s -d=doc -t=doc/template jsdeferred.js}
+ if JSDOC.exist? && system('which java')
+ sh %{java -jar #{JSDOC}/jsrun.jar #{JSDOC}/app/run.js -s -d=doc -t=doc/template jsdeferred.js}
+ else
+ warn "java is not installed or #{JSDOC} is not exists."
+ end
end
View
@@ -1,30 +1,65 @@
(function ($) {
- $.deferred = Deferred;
- $.fn.extend({
- deferred: function (name) {
- var args = Array.prototype.slice.call(arguments, 1);
- return Deferred.connect(this[name], { target:this }).apply(null, args);
- }
- });
- // override jQuery Ajax function for returning Deferred.
- var orig_ajax = $.ajax; $.ajax = function (opts) {
- var d = $.deferred(), orig = {};
- $.extend(orig, opts);
+ // Convert jQuery.Deferred or jqXHR to JSDeferred.
+ //
+ Deferred.absorb = function (obj) {
+ var ret = new Deferred();
+ ret.progress = function () {};
+ obj.done(function (v) {
+ // Remove isDeferred signature from jQuery.Deferred as "Result"
+ if (v.toJSDeferred) delete v.toJSDeferred;
+ ret.call(v);
+ });
+ obj.fail(function (v) {
+ // Remove isDeferred signature from jQuery.Deferred as "Result"
+ if (v.toJSDeferred) delete v.toJSDeferred;
+ ret.fail(v);
+ });
+ if (obj.progress) obj.progress(function (v) {
+ ret.progress(v);
+ });
+ return ret;
+ };
- opts.success = function () {
- if (orig.success) orig.success.apply(this, arguments);
- d.call.apply(d, arguments);
- };
+ // Fortunately, jQuery.Deferred and jqXHR in 1.7 does not have next() and error() function.
+ // So JSDeferred can implement it.
- opts.error = function () {
- if (orig.error) orig.error.apply(this, arguments);
- d.fail.apply(d, arguments);
+ var orig_Deferred = $.Deferred;
+ $.Deferred = function (fun) {
+ var ret = orig_Deferred.apply(this, arguments);
+ ret.toJSDeferred = function () {
+ return Deferred.absorb(this);
+ };
+ ret.next = function (fun) {
+ return Deferred.absorb(this).next(fun);
};
+ ret.error = function (fun) {
+ return Deferred.absorb(this).error(fun);
+ };
+ return ret;
+ };
- orig_ajax(opts);
+ var orig_ajax = $.ajax;
+ $.ajax = function () {
+ var ret = orig_ajax.apply(this, arguments);
+ ret.toJSDeferred = function () {
+ return Deferred.absorb(this);
+ };
+ ret.next = function (fun) {
+ return Deferred.absorb(this).next(fun);
+ };
+ ret.error = function (fun) {
+ return Deferred.absorb(this).error(fun);
+ };
+ return ret;
+ };
- return d;
+ // okay, jQuery.Deferred's interface is compatible to JSDeferred.
+ var orig_isDeferred = Deferred.isDeferred;
+ Deferred.isDeferred = function (obj) {
+ return orig_isDeferred(obj) || !!(obj && obj.toJSDeferred);
};
+
+ $.JSDeferred = Deferred;
})(jQuery);
View
@@ -157,7 +157,7 @@ Deferred.chain = function () {
Deferred.wait = function (n) {
var d = new Deferred(), t = new Date();
var id = setTimeout(function () {
- d.call((new Date).getTime() - t.getTime());
+ d.call((new Date()).getTime() - t.getTime());
}, n * 1000);
d.canceller = function () { clearTimeout(id) };
return d;
@@ -171,14 +171,20 @@ Deferred.call = function (fun) {
};
Deferred.parallel = function (dl) {
- if (arguments.length > 1) dl = Array.prototype.slice.call(arguments);
+ var isArray = false;
+ if (arguments.length > 1) {
+ dl = Array.prototype.slice.call(arguments);
+ isArray = true;
+ } else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
+ isArray = true;
+ }
var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
- if (typeof d == "function") d = Deferred.next(d);
+ if (typeof d == "function") dl[i] = d = Deferred.next(d);
d.next(function (v) {
values[i] = v;
if (--num <= 0) {
- if (dl instanceof Array) {
+ if (isArray) {
values.length = dl.length;
values = Array.prototype.slice.call(values, 0);
}
@@ -200,12 +206,18 @@ Deferred.parallel = function (dl) {
};
Deferred.earlier = function (dl) {
- if (arguments.length > 1) dl = Array.prototype.slice.call(arguments);
+ var isArray = false;
+ if (arguments.length > 1) {
+ dl = Array.prototype.slice.call(arguments);
+ isArray = true;
+ } else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
+ isArray = true;
+ }
var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
d.next(function (v) {
values[i] = v;
- if (dl instanceof Array) {
+ if (isArray) {
values.length = dl.length;
values = Array.prototype.slice.call(values, 0);
}
@@ -322,7 +334,7 @@ Deferred.connect = function (funo, options) {
}
Deferred.next(function () { func.apply(target, args) });
return d;
- }
+ };
};
Deferred.Arguments = function (args) { this.args = Array.prototype.slice.call(args, 0) };
View
@@ -155,7 +155,7 @@ Deferred.chain = function () {
Deferred.wait = function (n) {
var d = new Deferred(), t = new Date();
var id = setTimeout(function () {
- d.call((new Date).getTime() - t.getTime());
+ d.call((new Date()).getTime() - t.getTime());
}, n * 1000);
d.canceller = function () { clearTimeout(id) };
return d;
@@ -169,14 +169,20 @@ Deferred.call = function (fun) {
};
Deferred.parallel = function (dl) {
- if (arguments.length > 1) dl = Array.prototype.slice.call(arguments);
+ var isArray = false;
+ if (arguments.length > 1) {
+ dl = Array.prototype.slice.call(arguments);
+ isArray = true;
+ } else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
+ isArray = true;
+ }
var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
- if (typeof d == "function") d = Deferred.next(d);
+ if (typeof d == "function") dl[i] = d = Deferred.next(d);
d.next(function (v) {
values[i] = v;
if (--num <= 0) {
- if (dl instanceof Array) {
+ if (isArray) {
values.length = dl.length;
values = Array.prototype.slice.call(values, 0);
}
@@ -198,12 +204,18 @@ Deferred.parallel = function (dl) {
};
Deferred.earlier = function (dl) {
- if (arguments.length > 1) dl = Array.prototype.slice.call(arguments);
+ var isArray = false;
+ if (arguments.length > 1) {
+ dl = Array.prototype.slice.call(arguments);
+ isArray = true;
+ } else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
+ isArray = true;
+ }
var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
d.next(function (v) {
values[i] = v;
- if (dl instanceof Array) {
+ if (isArray) {
values.length = dl.length;
values = Array.prototype.slice.call(values, 0);
}
@@ -320,7 +332,7 @@ Deferred.connect = function (funo, options) {
}
Deferred.next(function () { func.apply(target, args) });
return d;
- }
+ };
};
Deferred.Arguments = function (args) { this.args = Array.prototype.slice.call(args, 0) };
@@ -361,29 +373,56 @@ Deferred.define = function (obj, list) {
this.Deferred = Deferred;
(function ($) {
- $.deferred = Deferred;
- $.fn.extend({
- deferred: function (name) {
- var args = Array.prototype.slice.call(arguments, 1);
- return Deferred.connect(this[name], { target:this }).apply(null, args);
- }
- });
- var orig_ajax = $.ajax; $.ajax = function (opts) {
- var d = $.deferred(), orig = {};
- $.extend(orig, opts);
+ Deferred.absorb = function (obj) {
+ var ret = new Deferred();
+ ret.progress = function () {};
+ obj.done(function (v) {
+ if (v.toJSDeferred) delete v.toJSDeferred;
+ ret.call(v);
+ });
+ obj.fail(function (v) {
+ if (v.toJSDeferred) delete v.toJSDeferred;
+ ret.fail(v);
+ });
+ if (obj.progress) obj.progress(function (v) {
+ ret.progress(v);
+ });
+ return ret;
+ };
- opts.success = function () {
- if (orig.success) orig.success.apply(this, arguments);
- d.call.apply(d, arguments);
+ var orig_Deferred = $.Deferred;
+ $.Deferred = function (fun) {
+ var ret = orig_Deferred.apply(this, arguments);
+ ret.toJSDeferred = function () {
+ return Deferred.absorb(this);
};
-
- opts.error = function () {
- if (orig.error) orig.error.apply(this, arguments);
- d.fail.apply(d, arguments);
+ ret.next = function (fun) {
+ return Deferred.absorb(this).next(fun);
};
+ ret.error = function (fun) {
+ return Deferred.absorb(this).error(fun);
+ };
+ return ret;
+ };
- orig_ajax(opts);
-
- return d;
+ var orig_ajax = $.ajax;
+ $.ajax = function () {
+ var ret = orig_ajax.apply(this, arguments);
+ ret.toJSDeferred = function () {
+ return Deferred.absorb(this);
+ };
+ ret.next = function (fun) {
+ return Deferred.absorb(this).next(fun);
+ };
+ ret.error = function (fun) {
+ return Deferred.absorb(this).error(fun);
+ };
+ return ret;
};
+ var orig_isDeferred = Deferred.isDeferred;
+ Deferred.isDeferred = function (obj) {
+ return orig_isDeferred(obj) || !!(obj && obj.toJSDeferred);
+ };
+
+ $.JSDeferred = Deferred;
})(jQuery);
View
@@ -383,7 +383,7 @@ Deferred.chain = function () {
Deferred.wait = function (n) {
var d = new Deferred(), t = new Date();
var id = setTimeout(function () {
- d.call((new Date).getTime() - t.getTime());
+ d.call((new Date()).getTime() - t.getTime());
}, n * 1000);
d.canceller = function () { clearTimeout(id) };
return d;
@@ -459,7 +459,7 @@ Deferred.parallel = function (dl) {
}
var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
- if (typeof d == "function") d = Deferred.next(d);
+ if (typeof d == "function") dl[i] = d = Deferred.next(d);
d.next(function (v) {
values[i] = v;
if (--num <= 0) {
@@ -697,7 +697,7 @@ Deferred.connect = function (funo, options) {
}
Deferred.next(function () { func.apply(target, args) });
return d;
- }
+ };
};
/**
* Used for Deferred.connect to allow to pass multiple values to next.
Oops, something went wrong.

0 comments on commit b88ed12

Please sign in to comment.