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

Commit b35efdd

Browse files
committed
polymer-body now requires use of a template element inside it.
1 parent 4b38cdf commit b35efdd

File tree

3 files changed

+100
-39
lines changed

3 files changed

+100
-39
lines changed

src/instance/base.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
}
133133
},
134134
// utility function that stamps a <template> into light-dom
135-
lightFromTemplate: function(template) {
135+
lightFromTemplate: function(template, refNode) {
136136
if (template) {
137137
// TODO(sorvell): mark this element as a lightDOMController so that
138138
// event listeners on bound nodes inside it will be called on it.
@@ -145,14 +145,18 @@
145145
// e.g. to prevent <img src="images/{{icon}}"> from generating a 404.
146146
var dom = this.instanceTemplate(template);
147147
// append to shadow dom
148-
this.appendChild(dom);
148+
if (refNode) {
149+
this.insertBefore(dom, refNode);
150+
} else {
151+
this.appendChild(dom);
152+
}
149153
// perform post-construction initialization tasks on ahem, light root
150-
this.shadowRootReady(this, template);
154+
this.shadowRootReady(this);
151155
// return the created shadow root
152156
return dom;
153157
}
154158
},
155-
shadowRootReady: function(root, template) {
159+
shadowRootReady: function(root) {
156160
// locate nodes with id and store references to them in this.$ hash
157161
this.marshalNodeReferences(root);
158162
// set up pointer gestures

src/polymer-body.html

+71-22
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,81 @@
1-
<polymer-element name="polymer-body" extends="body">
1+
<!--
2+
Copyright 2014 The Polymer Authors. All rights reserved.
3+
Use of this source code is governed by a BSD-style
4+
license that can be found in the LICENSE file.
5+
-->
6+
<!--
7+
/**
8+
* @module Polymer
9+
*/
10+
/**
11+
* The `polymer-body` element extends the body element. It provides a quick and
12+
* easy way to do data binding without the need to setup a custom element
13+
* or template. The body itself serves as the model and controller for the
14+
* elements it contains. Both data and event handlers can be bound.
15+
*
16+
* The `polymer-body` element does not create any ShadowDOM. Instead,
17+
* it creates elements in the main document. This makes it convenient to use
18+
* when it's important that elements are not placed inside ShadowDOM.
19+
*
20+
* The `polymer-body` element provides a subset of the capability you
21+
* get when you define a custom element. When you put data and event listeners
22+
* on `polymer-body`, you're customizing an instance of the element.
23+
* Because of this you cannot define lifecycle methods like ready. If you need
24+
* to do this, make a custom element.
25+
*
26+
* Example:
27+
*
28+
* <body is="polymer-body">
29+
* <template>
30+
* {{greeting}}
31+
* <button on-tap="buttonTap">Tap me!</button>
32+
* </template>
33+
* <script>
34+
* model.greeting = 'Hello!';
35+
* model.buttonTap = function() {
36+
* console.log('tap!');
37+
* };
38+
* </script>
39+
* </body>
40+
*
41+
* @status stable
42+
-->
43+
<polymer-element name="polymer-body" extends="body" attributes="modelName">
244

345
<script>
4-
5-
// upgrade polymer-body last so that it can contain other imported elements
6-
document.addEventListener('polymer-ready', function() {
746

8-
Polymer('polymer-body', Platform.mixin({
9-
10-
created: function() {
11-
this.template = document.createElement('template');
12-
var body = wrap(document).body;
13-
var c$ = body.childNodes.array();
14-
for (var i=0, c; (c=c$[i]); i++) {
15-
if (c.localName !== 'script') {
16-
this.template.content.appendChild(c);
47+
/*
48+
Normally, it would be required to load polymer-body last to ensure
49+
its contained elements bind properly. However, for convenience, we load
50+
`polymer-body` with polymer and register it at `polymer-ready` time.
51+
This means that `polymer-body` is ready asynchronous to the
52+
`polymer-ready` event.
53+
*/
54+
// upgrade polymer-body last so that
55+
// 1. it can contain other imported elements without needing to come last
56+
// 2. scripts that customize the model are not required to be before
57+
// polymer-body is loaded (note: native imports block scripts).
58+
document.addEventListener('polymer-ready', function() {
59+
60+
Polymer('polymer-body', {
61+
62+
modelName: 'model',
63+
64+
ready: function() {
65+
this.template = document.querySelector('template');
66+
var model = window[this.modelName];
67+
if (model) {
68+
Platform.mixin(this, model);
69+
}
70+
if (this.template) {
71+
this.lightFromTemplate(this.template,
72+
this.template.nextElementSibling);
1773
}
1874
}
19-
// snarf up user defined model
20-
window.model = this;
21-
},
22-
23-
parseDeclaration: function(elementElement) {
24-
this.lightFromTemplate(this.template);
25-
}
2675

27-
}, window.model));
76+
});
2877

29-
});
78+
});
3079

3180
</script>
3281

test/html/polymer-body.html

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
<!doctype html>
22
<html>
33
<head>
4-
<title>async test</title>
4+
<title>polymer-body test</title>
55
<script src="../../../platform/platform.js"></script>
66
<link rel="import" href="../../polymer.html">
77
<script src="../../../tools/test/htmltest.js"></script>
88
<script src="../../../tools/test/chai/chai.js"></script>
99
</head>
1010
<body is="polymer-body" unresolved>
11-
<h2 on-tap="{{eventAction}}">{{greeting}}</h2>
11+
<div>top</div>
12+
13+
<template>
14+
<h2 id="h" on-tap="{{eventAction}}">{{greeting}}</h2>
15+
</template>
16+
17+
<div>bottom</div>
18+
1219
<script>
1320
model = {
14-
greeting: 'Hi!',
15-
ready: function() {
16-
var h = document.body.querySelector('h2');
21+
greeting: 'Hi',
22+
eventAction: function(e) {
23+
e.handled = true;
24+
},
25+
test: function() {
26+
var h = this.$.h;
1727
chai.assert.equal(h.textContent, this.greeting, 'binding applied');
1828
var e = this.fire('tap', {}, h);
1929
chai.assert.isTrue(e.handled, 'element event handler fired');
20-
e = this.fire('pointerdown');
21-
chai.assert.isTrue(e.handled, 'host event handler fired');
2230
done();
23-
},
24-
eventAction: function(e) {
25-
e.handled = true;
26-
},
27-
eventDelegates: {
28-
pointerdown: 'eventAction'
2931
}
3032
}
33+
34+
addEventListener('polymer-ready', function() {
35+
wrap(document.body).async('test');
36+
});
37+
3138
</script>
39+
3240
</body>
3341
</html>

0 commit comments

Comments
 (0)