Skip to content

Commit

Permalink
Merge pull request madrobby#190 from johnboxall/zepto
Browse files Browse the repository at this point in the history
---

Add end() support to Zepto by wrapping methods that change the working elements.

Conflicts:
	test/zepto.html
  • Loading branch information
madrobby committed Jul 1, 2011
2 parents b33bf64 + ca6f41d commit ce072be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/zepto.js
Expand Up @@ -106,6 +106,9 @@ var Zepto = (function() {
return $$(element.parentNode, selector).indexOf(element) >= 0;
}));
},
end: function(){
return this.prevObject || $();
},
add:function(selector,context){
return $(uniq(this.concat($(selector,context))));
},
Expand Down Expand Up @@ -306,6 +309,15 @@ var Zepto = (function() {
}
};

'filter,add,not,eq,first,last,find,closest,parents,parent,children,siblings'.split(',').forEach(function(property){
var fn = $.fn[property];
$.fn[property] = function() {
var ret = fn.apply(this, arguments);
ret.prevObject = this;
return ret;
}
});

['width', 'height'].forEach(function(property){
$.fn[property] = function(){ var offset = this.offset(); return offset ? offset[property] : null }
});
Expand Down
14 changes: 14 additions & 0 deletions test/zepto.html
Expand Up @@ -173,6 +173,11 @@ <h1>Zepto DOM unit tests</h1>
<div class="eq1"></div>
<div class="eq2"></div>
</div>

<div id="end_test">
<div class="end_one"><b><span></span></b></div>
<div class="end_two"><b><span>1</span><span>2</span></b></div>
</div>

<script>

Expand Down Expand Up @@ -1321,6 +1326,15 @@ <h1>Zepto DOM unit tests</h1>
t.assertEqual($els.slice().length, 3);
t.assertEqual(typeof $els.slice().ready, 'function');
t.assertEqual($els.slice(-1)[0].className, 'slice3');
},

testEnd: function (t) {
t.assert($().end().length, 0);

var $endTest = $('#end_test');
var $endTest2 = $('#end_test').find('div').find('span').end().end();
t.assertEqual($endTest.length, $endTest2.length);
t.assertEqual($endTest.get(0), $endTest2.get(0));
}
});
</script>
Expand Down

0 comments on commit ce072be

Please sign in to comment.