Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Add 'connected' event for custom elements #758

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/customElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function getWidgetPropertyFromAttribute(attributeName: string, attributeValue: s
return [ propertyName, value ];
}

let customEventClass = global.CustomEvent;
export let customEventClass = global.CustomEvent;

if (typeof customEventClass !== 'function') {
const customEvent = function (event: string, params: any) {
Expand Down
4 changes: 4 additions & 0 deletions src/registerCustomElement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
customEventClass,
CustomElementDescriptor,
handleAttributeChanged,
initializeElement
Expand Down Expand Up @@ -43,6 +44,9 @@ export function registerCustomElement(descriptorFactory: CustomElementDescriptor
if (!this._isAppended) {
this._appender();
this._isAppended = true;
this.dispatchEvent(new customEventClass('connected', {
bubbles: false
}));
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/functional/registerCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ registerSuite('registerCustomElement', {
assert.isTrue(buttonClicked);
});
},
'custom elements emit event on connection'() {
if (skip) {
this.skip('not compatible with iOS 9.1 or Safari 9.1');
}
return this.remote
.get((<any> require).toUrl('./support/registerCustomElement.html'))
.setFindTimeout(1000)
.findByCssSelector('#testButton > button')
.click()
.end()
.execute('return window.connectedEvent')
.then((connectedEvent: boolean) => {
assert.isTrue(connectedEvent);
});
},
'updates the correct instance when multiple or the same custom elements are used'() {
if (skip) {
this.skip('not compatible with iOS 9.1 or Safari 9.1');
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/support/registerCustomElement.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<script>
window.buttonClicked = false;
window.ready = false;
window.connectedEvent = false;

require.config({
packages: [
Expand All @@ -28,6 +29,10 @@
]
});

document.getElementById('testButton').addEventListener('connected', function() {
window.connectedEvent = true;
});

require(['./registerCustomElement'], function() {
window.ready = true;

Expand Down