Permalink
Browse files

changed `fn.each()` to execute callback in scope of current element

Also swap arguments: index is first, element second.
This matches jQuery's `each()`
  • Loading branch information...
1 parent 329dda9 commit 951e71bc4578c7b3f4bcb803f28921697d745ab0 @mislav mislav committed Dec 20, 2010
Showing with 39 additions and 34 deletions.
  1. +9 −9 src/event.js
  2. +21 −20 src/zepto.js
  3. +9 −5 test/zepto.html
View
@@ -50,13 +50,13 @@
};
$.fn.bind = function(event, callback){
- return this.each(function(element){
- add(element, event, callback);
+ return this.each(function(){
+ add(this, event, callback);
});
};
$.fn.unbind = function(event, callback){
- return this.each(function(element){
- remove(element, event, callback);
+ return this.each(function(){
+ remove(this, event, callback);
});
};
@@ -70,7 +70,7 @@
}
$.fn.delegate = function(selector, event, callback){
- return this.each(function(element){
+ return this.each(function(i, element){
add(element, event, callback, selector, function(e){
var target = e.target, nodes = $$(element, selector);
while (target && nodes.indexOf(target) < 0) target = target.parentNode;
@@ -83,8 +83,8 @@
});
};
$.fn.undelegate = function(selector, event, callback){
- return this.each(function(element){
- remove(element, event, callback, selector);
+ return this.each(function(){
+ remove(this, event, callback, selector);
});
}
@@ -98,9 +98,9 @@
};
$.fn.trigger = function(event){
- return this.each(function(element){
+ return this.each(function(){
var e = document.createEvent('Events');
- element.dispatchEvent(e, e.initEvent(event, true, false));
+ this.dispatchEvent(e, e.initEvent(event, true, false));
});
};
})(Zepto);
View
@@ -50,10 +50,11 @@ var Zepto = (function() {
},
get: function(idx){ return idx === undefined ? this.dom : this.dom[idx] },
size: function(){ return this.length },
- remove: function(){
- return this.each(function(el){ el.parentNode.removeChild(el) });
+ remove: function(){ return this.each(function(){ this.parentNode.removeChild(this) }) },
+ each: function(callback){
+ this.dom.forEach(function(el, idx){ callback.call(el, idx, el) });
+ return this;
},
- each: function(callback){ this.dom.forEach(callback); return this },
filter: function(selector){
return $(this.dom.filter(function(element){
return $$(element.parentNode, selector).indexOf(element) >= 0;
@@ -90,8 +91,8 @@ var Zepto = (function() {
},
parent: function(selector){
var node, nodes = [];
- this.each(function(el){
- if ((node = el.parentNode) && nodes.indexOf(node) < 0) nodes.push(node);
+ this.each(function(){
+ if ((node = this.parentNode) && nodes.indexOf(node) < 0) nodes.push(node);
});
nodes = $(nodes);
return selector === undefined ? nodes : nodes.filter(selector);
@@ -104,20 +105,20 @@ var Zepto = (function() {
html: function(html){
return html === undefined ?
(this.length > 0 ? this.dom[0].innerHTML : null) :
- this.each(function(element){ element.innerHTML = html });
+ this.each(function(){ this.innerHTML = html });
},
text: function(text){
return text === undefined ?
(this.length > 0 ? this.dom[0].innerText : null) :
- this.each(function(element){ element.innerText = text });
+ this.each(function(){ this.innerText = text });
},
attr: function(name, value){
return (typeof name == 'string' && value === undefined) ?
(this.length > 0 && this.dom[0].nodeName === 'INPUT' && this.dom[0].type === 'text' && name === 'value') ? (this.dom[0].value) :
(this.length > 0 ? this.dom[0].getAttribute(name) || undefined : null) :
- this.each(function(element){
- if (typeof name == 'object') for (key in name) element.setAttribute(key, name[key])
- else element.setAttribute(name, value);
+ this.each(function(){
+ if (typeof name == 'object') for (key in name) this.setAttribute(key, name[key])
+ else this.setAttribute(name, value);
});
},
offset: function(){
@@ -134,7 +135,7 @@ var Zepto = (function() {
css = "";
for (key in property) css += key + ':' + property[key] + ';';
if (typeof property == 'string') css = property + ":" + value;
- return this.each(function(element) { element.style.cssText += ';' + css });
+ return this.each(function() { this.style.cssText += ';' + css });
},
index: function(element){
return this.dom.indexOf($(element).get(0));
@@ -143,19 +144,19 @@ var Zepto = (function() {
return classRE(name).test(this.dom[0].className);
},
addClass: function(name){
- return this.each(function(element){
- !$(element).hasClass(name) && (element.className += (element.className ? ' ' : '') + name)
+ return this.each(function(){
+ !$(this).hasClass(name) && (this.className += (this.className ? ' ' : '') + name)
});
},
removeClass: function(name){
- return this.each(function(element){
- element.className = element.className.replace(classRE(name), ' ').trim()
+ return this.each(function(){
+ this.className = this.className.replace(classRE(name), ' ').trim()
});
},
toggleClass: function(name, when){
- return this.each(function(element){
- ((when !== undefined && !when) || $(element).hasClass(name)) ?
- $(element).removeClass(name) : $(element).addClass(name)
+ return this.each(function(){
+ ((when !== undefined && !when) || $(this).hasClass(name)) ?
+ $(this).removeClass(name) : $(this).addClass(name)
});
}
};
@@ -170,8 +171,8 @@ var Zepto = (function() {
for (key in adjacencyOperators)
$.fn[key] = (function(operator) {
return function(html){
- return this.each(function(element){
- element['insertAdjacent' + (html instanceof Element ? 'Element' : 'HTML')](operator, html);
+ return this.each(function(){
+ this['insertAdjacent' + (html instanceof Element ? 'Element' : 'HTML')](operator, html);
});
};
})(adjacencyOperators[key]);
View
@@ -242,9 +242,13 @@ <h1>Zepto DOM unit tests</h1>
},
testEach: function(t){
- var tagnames = [];
- $('h1,p').each(function(e){ tagnames.push(e.tagName) });
- t.assertEqual('H1PPPP', tagnames.join(''));
+ var index, tagnames = [];
+ $('h1,p').each(function(idx, el){
+ index = idx;
+ if (this == el) tagnames.push(el.tagName);
+ });
+ t.assertEqual('H1, P, P, P, P', tagnames.join(', '));
+ t.assertEqual(4, index);
},
testFirst: function(t){
@@ -692,14 +696,14 @@ <h1>Zepto DOM unit tests</h1>
var el = $('#some_element').get(0);
$.fn.plugin = function(){
- return this.each(function(el){ el.innerHTML = 'plugin!' });
+ return this.each(function(){ this.innerHTML = 'plugin!' });
};
$('#some_element').plugin();
t.assertEqual('plugin!', el.innerHTML);
// test if existing Zepto objects receive new plugins
$.fn.anotherplugin = function(){
- return this.each(function(el){ el.innerHTML = 'anotherplugin!' });
+ return this.each(function(){ this.innerHTML = 'anotherplugin!' });
}
t.assert(typeof $('#some_element').anotherplugin == 'function');
$('#some_element').anotherplugin();

0 comments on commit 951e71b

Please sign in to comment.