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

Commit

Permalink
add 'domReady' method. This method can be used to access elements in …
Browse files Browse the repository at this point in the history
…dom (descendants, ancestors, siblings) such that the developer is inured to upgrade ordering. If the element definitions have loaded, domReady can be used to access upgraded elements.
  • Loading branch information
sorvell committed Feb 19, 2014
1 parent 19f854e commit 1b94222
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/instance/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
if (this.enteredView) {
this.enteredView();
}
// NOTE: domReady can be used to access elements in dom (descendants,
// ancestors, siblings) such that the developer is enured to upgrade
// ordering. If the element definitions have loaded, domReady
// can be used to access upgraded elements.
if (!this.hasBeenAttached) {
this.hasBeenAttached = true;
if (this.domReady) {
this.async('domReady');
}
}
},
detachedCallback: function() {
if (!this.preventDispose) {
Expand Down
60 changes: 60 additions & 0 deletions test/html/ctor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!doctype html>
<html>
<head>
<title>constructor test</title>
<script src="../../../platform/platform.js"></script>
<link rel="import" href="../../polymer.html">
<script src="../../../tools/test/htmltest.js"></script>
<script src="../../../tools/test/chai/chai.js"></script>
</head>
<body>

<x-foo></x-foo>
<x-bar></x-bar>

<polymer-element name="x-foo" constructor="XFoo">
<template>
I am x-foo.
</template>
<script>
(function() {
var nog = function() {};

Polymer('x-foo', {
created: function() {
chai.assert.isTrue(this instanceof
Object.getPrototypeOf(this).constructor);
},
domReady: function() {
chai.assert.isDefined(window.XFoo, 'constructor is undefined');
chai.assert.isTrue(CustomElements.instanceof(this, XFoo), 'instanceof failed');
chai.assert.isFalse(CustomElements.instanceof(this, nog), 'instanceof bugus base succeeded');
}
});
})();
</script>
</polymer-element>

<polymer-element name="x-bar" extends="x-foo" constructor="XBar">
<template>
I am x-bar.
</template>
<script>
Polymer('x-bar', {
created: function() {
this.super();
chai.assert.isTrue(this instanceof
Object.getPrototypeOf(this).constructor);
},
domReady: function() {
this.super();
chai.assert.isDefined(window.XBar, 'constructor is undefined');
//chai.assert.isTrue(this.impl instanceof XBar, 'instanceof failed');
chai.assert.isTrue(CustomElements.instanceof(this, XBar), 'instanceof failed');
done();
}
});
</script>
</polymer-element>
</body>
</html>
45 changes: 45 additions & 0 deletions test/html/domready.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!doctype html>
<html>
<head>
<title>domReady test</title>
<script src="../../../platform/platform.js"></script>
<link rel="import" href="../../polymer.html">
<script src="../../../tools/test/htmltest.js"></script>
<script src="../../../tools/test/chai/chai.js"></script>
</head>
<body>

<x-bar></x-bar>
<x-foo>
<x-bar></x-bar>
</x-foo>
<x-bar></x-bar>

<polymer-element name="x-foo">
<template>
I am x-foo.
</template>
<script>
Polymer('x-foo', {
fooish: true,
domReady: function() {
chai.assert.isTrue(this.firstElementChild.isBar, 'child is not upgraded');
chai.assert.isTrue(this.previousElementSibling.isBar, 'sibling is not upgraded');
chai.assert.isTrue(this.nextElementSibling.isBar, 'sibling is not upgraded');
done();
}
});
</script>
</polymer-element>

<polymer-element name="x-bar">
<template>
I am x-bar.
</template>
<script>
Polymer('x-bar', {
isBar: true
});
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions test/js/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ htmlSuite('element registration', function() {
htmlTest('html/element-registration.html');
htmlTest('html/element-import.html');
htmlTest('html/polymer-body.html');
htmlTest('html/ctor.html');
htmlTest('html/domready.html');
});

0 comments on commit 1b94222

Please sign in to comment.