OLD | NEW |
| (Empty) |
1 <!-- | |
2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | |
3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
6 Code distributed by Google as part of the polymer project is also | |
7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
8 --> | |
9 | |
10 <!-- | |
11 `core-meta` provides a method of constructing a self-organizing database. | |
12 It is useful to collate element meta-data for things like catalogs and for | |
13 designer. | |
14 | |
15 Example, an element folder has a `metadata.html` file in it, that contains a | |
16 `core-meta`, something like this: | |
17 | |
18 <core-meta id="my-element" label="My Element"> | |
19 <property name="color" value="blue"></property> | |
20 </core-meta> | |
21 | |
22 An application can import as many of these files as it wants, and then use | |
23 `core-meta` again to access the collected data. | |
24 | |
25 <script> | |
26 var meta = document.createElement('core-meta'); | |
27 console.log(meta.list); // dump a list of all meta-data elements that have
been created | |
28 </script> | |
29 | |
30 Use `byId(id)` to retrive a specific core-meta. | |
31 | |
32 <script> | |
33 var meta = document.createElement('core-meta'); | |
34 console.log(meta.byId('my-element')); | |
35 </script> | |
36 | |
37 By default all meta-data are stored in a single databse. If your meta-data | |
38 have different types and want them to be stored separately, use `type` to | |
39 differentiate them. | |
40 | |
41 Example: | |
42 | |
43 <core-meta id="x-foo" type="xElt"></core-meta> | |
44 <core-meta id="x-bar" type="xElt"></core-meta> | |
45 <core-meta id="y-bar" type="yElt"></core-meta> | |
46 | |
47 <script> | |
48 var meta = document.createElement('core-meta'); | |
49 meta.type = 'xElt'; | |
50 console.log(meta.list); | |
51 </script> | |
52 | |
53 @group Polymer Core Elements | |
54 @element core-meta | |
55 @homepage github.io | |
56 --> | |
57 | |
58 <link rel="import" href="../polymer/polymer.html"> | |
59 | |
60 <polymer-element name="core-meta" attributes="label type" hidden> | |
61 <script> | |
62 | |
63 (function() { | |
64 | |
65 var SKIP_ID = 'meta'; | |
66 var metaData = {}, metaArray = {}; | |
67 | |
68 Polymer('core-meta', { | |
69 | |
70 /** | |
71 * The type of meta-data. All meta-data with the same type with be | |
72 * stored together. | |
73 * | |
74 * @attribute type | |
75 * @type string | |
76 * @default 'default' | |
77 */ | |
78 type: 'default', | |
79 | |
80 alwaysPrepare: true, | |
81 | |
82 ready: function() { | |
83 this.register(this.id); | |
84 }, | |
85 | |
86 get metaArray() { | |
87 var t = this.type; | |
88 if (!metaArray[t]) { | |
89 metaArray[t] = []; | |
90 } | |
91 return metaArray[t]; | |
92 }, | |
93 | |
94 get metaData() { | |
95 var t = this.type; | |
96 if (!metaData[t]) { | |
97 metaData[t] = {}; | |
98 } | |
99 return metaData[t]; | |
100 }, | |
101 | |
102 register: function(id, old) { | |
103 if (id && id !== SKIP_ID) { | |
104 this.unregister(this, old); | |
105 this.metaData[id] = this; | |
106 this.metaArray.push(this); | |
107 } | |
108 }, | |
109 | |
110 unregister: function(meta, id) { | |
111 delete this.metaData[id || meta.id]; | |
112 var i = this.metaArray.indexOf(meta); | |
113 if (i >= 0) { | |
114 this.metaArray.splice(i, 1); | |
115 } | |
116 }, | |
117 | |
118 /** | |
119 * Returns a list of all meta-data elements with the same type. | |
120 * | |
121 * @property list | |
122 * @type array | |
123 * @default [] | |
124 */ | |
125 get list() { | |
126 return this.metaArray; | |
127 }, | |
128 | |
129 /** | |
130 * Retrieves meta-data by ID. | |
131 * | |
132 * @method byId | |
133 * @param {String} id The ID of the meta-data to be returned. | |
134 * @returns Returns meta-data. | |
135 */ | |
136 byId: function(id) { | |
137 return this.metaData[id]; | |
138 } | |
139 | |
140 }); | |
141 | |
142 })(); | |
143 | |
144 </script> | |
145 </polymer-element> | |
OLD | NEW |