From d3fbe58e9486dfc2d65463ffb6054698e579cf79 Mon Sep 17 00:00:00 2001 From: Elliott Sprehn Date: Fri, 19 Dec 2014 14:01:49 -0800 Subject: [PATCH] Add shadowRootReady callback for SkyElement. This is called right after stamping the template into the ShadowRoot. This is a useful place to querySelector/getElementById for elements inside the ShadowRoot. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/817053002 --- framework/sky-element/sky-element.sky | 5 +++++ tests/framework/templates-expected.txt | 7 ++++--- tests/framework/templates.sky | 9 +++++++++ tests/resources/test-element.sky | 4 ++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/framework/sky-element/sky-element.sky b/framework/sky-element/sky-element.sky index aaf130faab8..19eb2535f90 100644 --- a/framework/sky-element/sky-element.sky +++ b/framework/sky-element/sky-element.sky @@ -44,6 +44,10 @@ class SkyElement extends HTMLElement { // override } + shadowRootReady() { + // override + } + createdCallback() { this.created(); } @@ -54,6 +58,7 @@ class SkyElement extends HTMLElement { if (template) { var shadow = this.ensureShadowRoot(); shadow.appendChild(template.createInstance(this)); + this.shadowRootReady(); } } this.attached(); diff --git a/tests/framework/templates-expected.txt b/tests/framework/templates-expected.txt index c79c8758738..efc7b2a4b14 100644 --- a/tests/framework/templates-expected.txt +++ b/tests/framework/templates-expected.txt @@ -1,7 +1,8 @@ -Running 3 tests +Running 4 tests ok 1 SkyElement templates should stamp when the element is inserted ok 2 SkyElement templates should connect data binding ok 3 SkyElement templates should connect event handlers -3 tests -3 pass +ok 4 SkyElement templates should call shadowRootReady after creating the template instance +4 tests +4 pass 0 fail diff --git a/tests/framework/templates.sky b/tests/framework/templates.sky index c0a9fa62089..30e119fc412 100644 --- a/tests/framework/templates.sky +++ b/tests/framework/templates.sky @@ -49,6 +49,15 @@ describe("SkyElement templates", function() { inside.dispatchEvent(event); assert.equal(element.lastEvent, event); }); + + it("should call shadowRootReady after creating the template instance", function() { + assert.equal(element.shadowRootReadyCount, 0); + sandbox.appendChild(element); + assert.equal(element.shadowRootReadyCount, 1); + element.remove(); + sandbox.appendChild(element); + assert.equal(element.shadowRootReadyCount, 1); + }); }); \ No newline at end of file diff --git a/tests/resources/test-element.sky b/tests/resources/test-element.sky index c852cd11f80..193b9e20674 100644 --- a/tests/resources/test-element.sky +++ b/tests/resources/test-element.sky @@ -14,10 +14,14 @@ module.exports = class extends SkyElement { created() { this.lastEvent = null; this.value = 10; + this.shadowRootReadyCount = 0; } handleEvent(event) { this.lastEvent = event; } + shadowRootReady() { + this.shadowRootReadyCount++; + } }.register();