Skip to content

Commit

Permalink
The second argument to Event.Handler callbacks is the target element …
Browse files Browse the repository at this point in the history
…when no selector is present, or the matching element when a selector is present. Callbacks are always bound to the original element.
  • Loading branch information
sstephenson committed May 7, 2010
1 parent f372474 commit b76ea83
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG
@@ -1,10 +1,12 @@
Fix issue where `Element.Layout#get` would fail to interpret negative pixel values. (Sebastien Gruhier, Andrew Dupont)
* The second argument to Event.Handler callbacks is the target element when no selector is present, or the matching element when a selector is present. Callbacks are always bound to the original element. (sam)

Fix bugs in layout.js. Add tests for `Element.Layout#toCSS`, `#toObject`, and `#toHash`. (RStankov, Andrew Dupont)
* Fix issue where `Element.Layout#get` would fail to interpret negative pixel values. (Sebastien Gruhier, Andrew Dupont)

Add `Element.Layout#toObject` and `Element.Layout.toHash`. (Andrew Dupont)
* Fix bugs in layout.js. Add tests for `Element.Layout#toCSS`, `#toObject`, and `#toHash`. (RStankov, Andrew Dupont)

Make `Element.Layout#toCSS` return camelized property names, as expected by `Element.setStyle`. [#1021 state:resolved] (njakobsen, Andrew Dupont)
* Add `Element.Layout#toObject` and `Element.Layout.toHash`. (Andrew Dupont)

* Make `Element.Layout#toCSS` return camelized property names, as expected by `Element.setStyle`. [#1021 state:resolved] (njakobsen, Andrew Dupont)

*1.7_rc1* (April 1, 2010)

Expand Down
5 changes: 2 additions & 3 deletions src/dom/event.js
Expand Up @@ -912,9 +912,8 @@
},

handleEvent: function(event) {
var element = this.selector ? event.findElement(this.selector) :
this.element;
if (element) this.callback.call(element, event, element);
var element = event.findElement(this.selector);
if (element) this.callback.call(this.element, event, element);
}
});

Expand Down
99 changes: 99 additions & 0 deletions test/unit/event_handler_test.js
@@ -0,0 +1,99 @@
new Test.Unit.Runner((function() {
function handle(selector, callback) {
if (!callback) {
callback = selector;
selector = false;
}
return new Event.Handler("container", "test:event", selector, callback);
}

return {
testHandlersDoNothingIfStartHasNotBeenCalled: function() {
var fired = false;
this.handler = handle(function() { fired = true });

$("container").fire("test:event");
this.assert(!fired);
},

testHandlersAreFiredWhenStartIsCalled: function() {
var fired = false;
this.handler = handle(function() { fired = true });

this.handler.start();
this.assert(!fired);
$("container").fire("test:event");
this.assert(fired);
},

testHandlersDoNotFireAfterStartingAndThenStopping: function() {
var fired = 0;
this.handler = handle(function() { fired++ });

this.handler.start();
this.assertEqual(0, fired);
$("container").fire("test:event");
this.assertEqual(1, fired);
this.handler.stop();
$("container").fire("test:event");
this.assertEqual(1, fired);
},

testHandlersWithoutSelectorsPassTheTargetElementToCallbacks: function() {
var span = $("container").down("span");
this.handler = handle(function(event, element) {
this.assertEqual(span, element);
}.bind(this));

this.handler.start();
span.fire("test:event");
},

testHandlersWithSelectorsPassTheMatchedElementToCallbacks: function() {
var link = $("container").down("a"), span = link.down("span");
this.handler = handle("a", function(event, element) {
this.assertEqual(link, element);
}.bind(this));

this.handler.start();
span.fire("test:event");
},

testHandlersWithSelectorsDoNotCallTheCallbackIfNoMatchingElementIsFound: function() {
var paragraph = $("container").down("p", 1), fired = false;
this.handler = handle("a", function(event, element) { fired = true });

this.handler.start();
paragraph.fire("test:event");
this.assert(!fired);
},

testHandlerCallbacksAreBoundToTheOriginalElement: function() {
var span = $("container").down("span"), element;
this.handler = handle(function() { element = this });

this.handler.start();
span.fire("test:event");
this.assertEqual($("container"), element);
},

testCallingStartMultipleTimesDoesNotInstallMultipleObservers: function() {
var fired = 0;
this.handler = handle(function() { fired++ });

this.handler.start();
this.handler.start();
$("container").fire("test:event");
this.assertEqual(1, fired);
},

teardown: function() {
try {
this.handler.stop();
} catch (e) {
} finally {
delete this.handler;
}
}
}
})());
4 changes: 4 additions & 0 deletions test/unit/fixtures/event_handler.html
@@ -0,0 +1,4 @@
<div id="container" style="display: none">
<p>Here's <a href="#"><span>a link</span></a>.</p>
<p>And here's another paragraph.</p>
</div>

0 comments on commit b76ea83

Please sign in to comment.