Skip to content

Commit

Permalink
Apply listeners in constructor rather than ready
Browse files Browse the repository at this point in the history
Fixes #4394. This change allows elements to have "first crack" at their own events (via the `listeners` object) before handlers registered via `on-*` see events. This is both more expected and fixes an inconsistency with 1.x.
  • Loading branch information
Steven Orvell committed Dec 12, 2017
1 parent 8196483 commit 35e3c54
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/legacy/legacy-element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
/** @type {Object<string, Function>} */
this._debouncers;
this.created();
// Ensure listeners are applied immediately so that they are
// added before declarative event listeners. This allows an element to
// decorate itself via an event prior to any declarative listeners
// seeing the event. Note, this ensures compatibility with 1.x ordering.
this._applyListeners();
}

/**
Expand Down Expand Up @@ -180,7 +185,6 @@
*/
ready() {
this._ensureAttributes();
this._applyListeners();
super.ready();
}

Expand Down
8 changes: 7 additions & 1 deletion lib/utils/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,13 @@
*/
setTouchAction: function(node, value) {
if (HAS_NATIVE_TA) {
node.style.touchAction = value;
// NOTE: add touchAction async so that events can be added in
// custom element constructors. Otherwise we run afoult of custom
// elements restriction against settings attributes (style) in the
// constructor.
Polymer.Async.microTask.run(() => {
node.style.touchAction = value;
});
}
node[TOUCH_ACTION] = value;
},
Expand Down
14 changes: 14 additions & 0 deletions test/unit/events-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
this._removed = [];
},
handle: function(e) {
const order = e._handleOrder = e._handleOrder || [];
order.push(this.localName);
this._handled[e.currentTarget.localName] = e.type;
},
unlisten: function(node, eventName, handler) {
Expand Down Expand Up @@ -51,6 +53,18 @@
</script>
</dom-module>

<dom-module id="x-order">
<template>
<x-listeners id="inner" on-foo="handle"></x-listeners>
</template>
<script>
Polymer({
is: 'x-order',
behaviors: [EventLoggerImpl]
});
</script>
</dom-module>

<dom-module id="x-dynamic">
<template>
<div id="inner"></div>
Expand Down
17 changes: 17 additions & 0 deletions test/unit/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@

});

suite('listeners + on-*', function() {
suiteSetup(function() {
el = document.createElement('x-order');
document.body.appendChild(el);
});

suiteTeardown(function() {
document.body.removeChild(el);
});

test('listeners handled before on-* events', function() {
const e = el.$.inner.fire('foo', {});
assert.deepEqual(e._handleOrder, ['x-listeners', 'x-order']);
});

});

suite('dynamic', function() {

var options;
Expand Down

0 comments on commit 35e3c54

Please sign in to comment.