Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Update handle method so that 'this' refers to the wrapped element instan... #9

Closed
wants to merge 1 commit into from

3 participants

@GCheung55

...ce that is calling the handle method. Update tests and docs for handle.

@kamicane said: "since sometimes it's not needed at all and will be faster".

@GCheung55 GCheung55 Update handle method so that 'this' refers to the wrapped element ins…
…tance that is calling the handle method. Update tests and docs for handle.
cd2e983
@arian
Collaborator

Now I'm thinking about it, stuff like https://github.com/mootools/elements/blob/master/lib/events.js#L32 should be updated too. Also in delegation.js.

@arian
Collaborator

cazy idea, we could make it an argument, and check the arity of the passed function, and decide whether to wrap the element or not.

var callback = function(node, element){
    element.attribute();
};
$(element).handle(callback);
// ... later inside handle
if (callback.length == 1) callback.call(this, this[0]);
else (callback.length == 2) callback.call(this, this[0], $(this[0]);

not sure about browser support, but I believe this works in all browsers, except Opera mini or something...

@kamicane kamicane closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 31, 2012
  1. @GCheung55

    Update handle method so that 'this' refers to the wrapped element ins…

    GCheung55 authored
    …tance that is calling the handle method. Update tests and docs for handle.
This page is out of date. Refresh to see the latest.
View
12 doc/elements.html
@@ -142,12 +142,16 @@
</ul>
<h3>Example</h3>
<pre><code class="javascript"><span class="keyword">var</span> checks = checkboxes.handle(<span class="keyword">function</span>(checkbox, index, buffer){
+ <span class="comment">// The context ('this') is the elements instance containing checkboxes</span>
+
<span class="comment">// checkbox is the native element</span>
- <span class="keyword">var</span> checked = checkbox.checked
- checkbox.checked = !checked
+ <span class="comment">// wrap it to use elements methods</span>
+ checkbox = $(checkbox)
+
+ <span class="keyword">var</span> checked = checkbox.checked()
+ checkbox.checked(!checked)
- <span class="comment">// "this" is the wrapped element object</span>
- <span class="keyword">this</span>.attribute(<span class="string">'data-checked'</span>, !checked)
+ checkbox.attribute(<span class="string">'data-checked'</span>, !checked)
<span class="comment">// return the checked checkboxes</span>
<span class="keyword">if</span> (checked) <span class="keyword">return</span> checkbox
View
13 doc/elements.md
@@ -103,16 +103,21 @@ loop. The returned boolean will not be included in the returned array.
```js
var checks = checkboxes.handle(function(checkbox, index, buffer){
+ // The context ('this') is the elements instance containing checkboxes
+
// checkbox is the native element
- var checked = checkbox.checked
- checkbox.checked = !checked
+ // wrap it to use elements methods
+ checkbox = $(checkbox)
+
+ var checked = checkbox.checked()
+ checkbox.checked(!checked)
- // "this" is the wrapped element object
- this.attribute('data-checked', !checked)
+ checkbox.attribute('data-checked', !checked)
// return the checked checkboxes
if (checked) return checkbox
}) // an array with the returned element
+
```
## remove
View
2  lib/elements.js
@@ -89,7 +89,7 @@ var elements = prime({
if (res != null && res !== false && res !== true) buffer.push(res)
} else for (var i = 0; i < length; i++){
var node = this[i]
- res = method.call($(node), node, i, buffer)
+ res = method.call(this, node, i, buffer)
if (res === false || res === true) break
if (res != null) buffer.push(res)
View
7 test/attributes.js
@@ -50,10 +50,11 @@ describe('attribute.js', function(){
it('should get correct attributes', function(){
var lis = $(document.getElementById('container').getElementsByTagName('li'))
lis.handle(function(element, index){
+ element = $(element)
switch (index){
- case 0: expect(this.getAttribute('class')).to.be('first'); break
- case 1: expect(this.getAttribute('title')).to.be('title'); break
- case 2: expect(this.getAttribute('id')).to.be('third'); break
+ case 0: expect(element.getAttribute('class')).to.be('first'); break
+ case 1: expect(element.getAttribute('title')).to.be('title'); break
+ case 2: expect(element.getAttribute('id')).to.be('third'); break
}
})
})
Something went wrong with that request. Please try again.