Permalink
Browse files

Allow context to be passed as a third argument to can.each.

  • Loading branch information...
Ralph Holzmann
Ralph Holzmann committed Jun 2, 2012
1 parent 19c9ca0 commit bbd2ad5e38df90f0ebcc09a20f7ea216fe20bd72
Showing with 18 additions and 14 deletions.
  1. +18 −14 util/array/each.js
View
@@ -1,18 +1,22 @@
steal(function(){
- can.each = function(elements, callback) {
+ can.each = function( elements, callback, context ) {
var i = 0, key;
- if ( elements ) {
- if (typeof elements.length == 'number' && elements.pop) {
- elements.attr && elements.attr('length');
- for(var len = elements.length; i < len; i++) {
- if(callback(elements[i], i, elements) === false) return elements;
- }
- } else {
- for(key in elements) {
- if(callback(elements[key], key) === false) return elements;
- }
- }
- }
+ if ( elements ) {
+ if ( typeof elements.length == 'number' && elements.pop ) {
+ elements.attr && elements.attr('length');
+ for ( var len = elements.length; i < len; i++ ) {
+ if ( callback.call( context, elements[i], i, elements ) === false ) {
+ break;
+ }
+ }
+ } else {
+ for ( key in elements ) {
+ if ( callback.call( context, elements[key], key ) === false ) {
+ break;
+ }
+ }
+ }
+ }
return elements;
}
-})
+});

0 comments on commit bbd2ad5

Please sign in to comment.