Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

minor fixes. example updated

  • Loading branch information...
commit 3d2d493b3fa08e2f6bd7b4298bb3205e320b8b13 1 parent 1f2265d
@dio-el-claire authored
Showing with 68 additions and 33 deletions.
  1. +60 −18 example/index.html
  2. +8 −15 jquery.waterfall.js
View
78 example/index.html
@@ -11,28 +11,70 @@
<script type="text/javascript" charset="utf-8">
$(function() {
$('.waterfall').click(function(e) {
+ console.log('first');
- var f1 = function() {
- return $.ajax({ url : 'post.php?arg=1'})
+ // series of 3 request - always done
+
+ $.waterfall(
+ function() {
+ return $.ajax({ url : 'post.php?arg=1'});
+ },
+ function() {
+ return $.ajax({ url : 'post.php?arg='+(1+parseInt(arguments[0]))});
},
- f2 = function(arg) {
- return parseInt(arg)+1;
+ function() {
+ return $.ajax({ url : 'post.php?arg=3'});
},
- f3 = function() {
- return $.ajax({ url : 'post.php?arg=3'+arguments[1]})
- };
-
+ function() {
+ return parseInt(arguments[0]) + parseInt(arguments[1]) + parseInt(arguments[2]);
+ }
+ )
+ .fail(function() {
+ console.log('error');
+ console.log(arguments)
+ $('.result').append('First waterfall failed. arguments number: '+arguments.length+'. Details see in console.<br>');
+ })
+ .done(function() {
+ console.log('success');
+ console.log(arguments)
+ $('.result').append('First waterfall success. arguments number: '+arguments.length+'. Details see in console.<br>');
+ });
+
+ console.log('second')
+
+ // check result after first request
$.waterfall(
- f1, f2, f3, 'Most important question in the Universe.', {answer : 42}, null, void(0), $.ajax({ url : 'post.php?arg=4'})
- ).fail(function() {
- console.log('error');
- console.log(arguments);
- $('.result').text('waterfall failed. arguments number: '+arguments.length+'. Details see in console.');
- }).done(function() {
- console.log('success');
- console.log(arguments);
- $('.result').text('waterfall success. arguments number: '+arguments.length+'. Details see in console.');
- })
+ function() {
+ return $.ajax({ url : 'post.php?ser=2&arg=1'});
+ },
+ function() {
+ return !(parseInt(arguments[0]) == 1);
+ },
+ function() {
+ return $.ajax({ url : 'post.php?ser=2&arg=3'});
+ }
+ )
+ .fail(function() {
+ console.log('error');
+ console.log(arguments)
+ $('.result').append('Second waterfall failed. arguments number: '+arguments.length+'. Details see in console.<br>');
+ })
+ .done(function() {
+ console.log('success');
+ console.log(arguments)
+ $('.result').text('Second waterfall success. arguments number: '+arguments.length+'. Details see in console.<br>');
+ });
+ // $.waterfall(
+ // f1, f2, f3, 'Most important question in the Universe.', {answer : 42}, null, void(0), $.ajax({ url : 'post.php?arg=4'})
+ // ).fail(function() {
+ // console.log('error');
+ // console.log(arguments);
+ // $('.result').text('waterfall failed. arguments number: '+arguments.length+'. Details see in console.');
+ // }).done(function() {
+ // console.log('success');
+ // console.log(arguments);
+ // $('.result').text('waterfall success. arguments number: '+arguments.length+'. Details see in console.');
+ // })
})
})
View
23 jquery.waterfall.js
@@ -2,14 +2,12 @@
/**
* Runs functions given in arguments in series, each functions passing their results to the next one.
* Return jQuery Deferred object.
- * При успешном завершении всех фунции их результаты передаются в resolve обработчик Deferred object.
- * Если одна из функций завершится с ошибкой будет вызван reject обработчик Deferred объекта.
*
* @example
* $.waterfall(
- * function() { $.ajax({url : first_url}) },
- * function() { $.ajax({url : second_url}) },
- * function() { $.ajax({url : another_url}) }
+ * function() { return $.ajax({url : first_url}) },
+ * function() { return $.ajax({url : second_url}) },
+ * function() { return $.ajax({url : another_url}) }
*).fail(function() {
* console.log(arguments)
*).done(function() {
@@ -30,15 +28,12 @@ $.waterfall = function() {
if (typeof(a) == 'function') {
if (!((d = a.apply(null, args)) && d.promise)) {
- d = $.Deferred()[d === false ? 'reject' : 'resolve'](d)
+ d = $.Deferred()[d === false ? 'reject' : 'resolve'](d);
}
} else if (a && a.promise) {
d = a;
- if (!d.isResolved() && !d.isRejected()) {
- d.resolve.apply(d, args);
- }
} else {
- d = $.Deferred().resolve(a);
+ d = $.Deferred()[a === false ? 'reject' : 'resolve'](a);
}
d.fail(function() {
@@ -48,11 +43,9 @@ $.waterfall = function() {
pointer++;
args.push(data);
- if (pointer == steps.length) {
- dfrd.resolve.apply(dfrd, args);
- } else {
- steps[pointer].apply(null, args);
- }
+ pointer == steps.length
+ ? dfrd.resolve.apply(dfrd, args)
+ : steps[pointer].apply(null, args);
});
});
});

0 comments on commit 3d2d493

Please sign in to comment.
Something went wrong with that request. Please try again.