refactored #each and #eachThen implementations
Showing
1 changed file
with
12 additions
and
19 deletions
... | @@ -574,11 +574,11 @@ Casper.prototype.download = function download(url, targetPath, method, data) { | ... | @@ -574,11 +574,11 @@ Casper.prototype.download = function download(url, targetPath, method, data) { |
574 | }; | 574 | }; |
575 | 575 | ||
576 | /** | 576 | /** |
577 | * Iterates over the values of a provided array and execute a callback | 577 | * Iterates over the values of a provided array and execute a callback for each |
578 | * for each item. | 578 | * item. |
579 | * | 579 | * |
580 | * @param Array array | 580 | * @param Array array |
581 | * @param Function fn Callback: function(self, item, index) | 581 | * @param Function fn Callback: function(casper, item, index) |
582 | * @return Casper | 582 | * @return Casper |
583 | */ | 583 | */ |
584 | Casper.prototype.each = function each(array, fn) { | 584 | Casper.prototype.each = function each(array, fn) { |
... | @@ -587,17 +587,14 @@ Casper.prototype.each = function each(array, fn) { | ... | @@ -587,17 +587,14 @@ Casper.prototype.each = function each(array, fn) { |
587 | this.log("each() only works with arrays", "error"); | 587 | this.log("each() only works with arrays", "error"); |
588 | return this; | 588 | return this; |
589 | } | 589 | } |
590 | (function _each(self) { | 590 | array.forEach(function _forEach(item, i) { |
591 | array.forEach(function _forEach(item, i) { | 591 | fn.call(this, this, item, i); |
592 | fn.call(self, self, item, i); | 592 | }, this); |
593 | }); | ||
594 | })(this); | ||
595 | return this; | 593 | return this; |
596 | }; | 594 | }; |
597 | 595 | ||
598 | /** | 596 | /** |
599 | * Iterates over the values of a provided array and adds a step | 597 | * Iterates over the values of a provided array and adds a step for each item. |
600 | * for each item. | ||
601 | * | 598 | * |
602 | * @param Array array | 599 | * @param Array array |
603 | * @param Function then Step: function(response); item will be attached to response.data | 600 | * @param Function then Step: function(response); item will be attached to response.data |
... | @@ -606,18 +603,14 @@ Casper.prototype.each = function each(array, fn) { | ... | @@ -606,18 +603,14 @@ Casper.prototype.each = function each(array, fn) { |
606 | Casper.prototype.eachThen = function each(array, then) { | 603 | Casper.prototype.eachThen = function each(array, then) { |
607 | "use strict"; | 604 | "use strict"; |
608 | if (!utils.isArray(array)) { | 605 | if (!utils.isArray(array)) { |
609 | this.log("each() only works with arrays", "error"); | 606 | this.log("eachThen() only works with arrays", "error"); |
610 | return this; | 607 | return this; |
611 | } | 608 | } |
612 | (function _each(self) { | 609 | array.forEach(function _forEach(item) { |
613 | array.forEach(function _forEach(item, i) { | 610 | this.then(function() { |
614 | self.then(function() { | 611 | this.then(this.createStep(then, {data: item})); |
615 | this.then(this.createStep(then, { | ||
616 | data: item | ||
617 | })); | ||
618 | }); | ||
619 | }); | 612 | }); |
620 | })(this); | 613 | }, this); |
621 | return this; | 614 | return this; |
622 | }; | 615 | }; |
623 | 616 | ... | ... |
-
Please register or sign in to post a comment