|
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"> |
2 | 44 |
|
3 | 45 | <script>
|
4 |
| - |
5 |
| - // upgrade polymer-body last so that it can contain other imported elements |
6 |
| - document.addEventListener('polymer-ready', function() { |
7 | 46 |
|
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); |
17 | 73 | }
|
18 | 74 | }
|
19 |
| - // snarf up user defined model |
20 |
| - window.model = this; |
21 |
| - }, |
22 |
| - |
23 |
| - parseDeclaration: function(elementElement) { |
24 |
| - this.lightFromTemplate(this.template); |
25 |
| - } |
26 | 75 |
|
27 |
| - }, window.model)); |
| 76 | + }); |
28 | 77 |
|
29 |
| - }); |
| 78 | + }); |
30 | 79 |
|
31 | 80 | </script>
|
32 | 81 |
|
|
0 commit comments