Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix jquery binding

  • Loading branch information...
commit fc23d0b028f732bf810b69c4bb4bcfca36864c37 1 parent c542c90
@cho45 authored
Showing with 50 additions and 45 deletions.
  1. +29 −35 binding/jquery.js
  2. +21 −10 test-jsdeferred.js
View
64 binding/jquery.js
@@ -1,7 +1,32 @@
(function ($) {
+ function wrap (obj) {
+ obj.toJSDeferred = function () {
+ return Deferred.absorb(this);
+ };
+ obj.next = function (fun) {
+ return Deferred.absorb(this).next(fun);
+ };
+ obj.error = function (fun) {
+ return Deferred.absorb(this).error(fun);
+ };
+ obj.done(function (v) {
+ if (obj._next) obj._next._fire('ok', v);
+ });
+ obj.fail(function (v) {
+ if (obj._next) obj._next._fire('ok', v);
+ });
+
+ var orig_promise = obj.promise;
+ obj.promise = function () {
+ return wrap(orig_promise.apply(this, arguments));
+ };
+
+ return obj;
+ }
+
// Convert jQuery.Deferred or jqXHR to JSDeferred.
- //
+
Deferred.absorb = function (obj) {
var ret = new Deferred();
ret.progress = function () {};
@@ -26,44 +51,13 @@
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);
- };
- ret.done(function (v) {
- if (ret._next) ret._next._fire('ok', v);
- });
- ret.fail(function (v) {
- if (ret._next) ret._next._fire('ok', v);
- });
- return ret;
+ return wrap(orig_Deferred.apply(this, arguments));
};
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);
- };
- ret.done(function (v) {
- if (ret._next) ret._next._fire('ok', v);
- });
- ret.fail(function (v) {
- if (ret._next) ret._next._fire('ok', v);
- });
- return ret;
+ // Currently, jQuery call deferred.promise( jqXHR ); so we just returns it.
+ return orig_ajax.apply(this, arguments);
};
// okay, jQuery.Deferred's interface is compatible to JSDeferred.
View
31 test-jsdeferred.js
@@ -914,34 +914,34 @@ next(function () {
return $.ajax({
url : "./test.html",
success : function () {
- ok("$.ajax#success");
+ ok("$.ajax#success 1");
},
error : function () {
- ng("$.ajax#success");
+ ng("$.ajax#success 1");
}
}).
next(function () {
- ok("$.ajax#success");
+ ok("$.ajax#success 2");
}).
error(function (e) {
- ng("$.ajax#success");
+ ng("$.ajax#success 2");
});
}).
next(function () {
return $.ajax({
url : "error-404" + Math.random(),
success : function () {
- ng("$.ajax#errro");
+ ng("$.ajax#error 1");
},
error : function () {
- ok("$.ajax#error", "You may see error on console but it is correct.");
+ ok("$.ajax#error 1", "You may see error on console but it is correct.");
}
}).
next(function () {
- ng("$.ajax#error");
+ ng("$.ajax#error 2");
}).
error(function (e) {
- ok("$.ajax#error");
+ ok("$.ajax#error 2");
});
}).
next(function () {
@@ -949,10 +949,20 @@ next(function () {
return $.get("./test.html");
}).
next(function () {
- ok("$.ajax#success");
+ ok("$.get#success");
}).
error(function (e) {
- ng("$.ajax#success");
+ ng("$.get#success");
+ });
+ }).
+ next(function () {
+ return next(function() {
+ return $(document.body).fadeTo(100, 0).fadeTo(100, 1).promise().next(function () {
+ ok("promise()");
+ });
+ }).
+ error(function (e) {
+ ng("promise()");
});
}).
next(function () {
@@ -983,6 +993,7 @@ next(function () {
return next(function () {
return next(function () {
msg("Calceling... No more tests below...");
+ ok("Done");
this.cancel();
}).
next(function () {
Please sign in to comment.
Something went wrong with that request. Please try again.