3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 Code distributed by Google as part of the polymer project is also
8 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 <link rel=
"import" href=
"../polymer/polymer.html">
14 `iron-meta` is a generic element you can use for sharing information across the DOM tree.
15 It uses [monostate pattern](http://c2.com/cgi/wiki?MonostatePattern) such that any
16 instance of iron-meta has access to the shared
17 information. You can use `iron-meta` to share whatever you want (or create an extension
18 [like x-meta] for enhancements).
20 The `iron-meta` instances containing your actual data can be loaded in an import,
21 or constructed in any way you see fit. The only requirement is that you create them
22 before you try to access them.
26 If I create an instance like this:
28 <iron-meta key="info" value="foo/bar"></iron-meta>
30 Note that value="foo/bar" is the metadata I've defined. I could define more
31 attributes or use child nodes to define additional metadata.
33 Now I can access that element (and it's metadata) from any iron-meta instance
34 via the byKey method, e.g.
36 meta.byKey('info').getAttribute('value').
38 Pure imperative form would be like:
40 document.createElement('iron-meta').byKey('info').getAttribute('value');
42 Or, in a Polymer element, you can include a meta in your template:
44 <iron-meta id="meta"></iron-meta>
46 this.$.meta.byKey('info').getAttribute('value');
62 Polymer
.IronMeta
= Polymer({
69 * The type of meta-data. All meta-data of the same type is stored
75 observer
: '_typeChanged'
79 * The key used to store `value` under the `type` namespace.
83 observer
: '_keyChanged'
87 * The meta-data to store or retrieve.
92 observer
: '_valueChanged'
96 * If true, `value` is set to the iron-meta instance itself.
100 observer
: '_selfChanged'
104 * Array of all meta-data values for the given type.
114 * Only runs if someone invokes the factory/constructor directly
115 * e.g. `new Polymer.IronMeta()`
117 factoryImpl: function(config
) {
119 for (var n
in config
) {
131 created: function() {
132 // TODO(sjmiles): good for debugging?
133 this._metaDatas
= metaDatas
;
134 this._metaArrays
= metaArrays
;
137 _keyChanged: function(key
, old
) {
138 this._resetRegistration(old
);
141 _valueChanged: function(value
) {
142 this._resetRegistration(this.key
);
145 _selfChanged: function(self
) {
151 _typeChanged: function(type
) {
152 this._unregisterKey(this.key
);
153 if (!metaDatas
[type
]) {
154 metaDatas
[type
] = {};
156 this._metaData
= metaDatas
[type
];
157 if (!metaArrays
[type
]) {
158 metaArrays
[type
] = [];
160 this.list
= metaArrays
[type
];
161 this._registerKeyValue(this.key
, this.value
);
165 * Retrieves meta data value by key.
168 * @param {string} key The key of the meta-data to be returned.
171 byKey: function(key
) {
172 return this._metaData
&& this._metaData
[key
];
175 _resetRegistration: function(oldKey
) {
176 this._unregisterKey(oldKey
);
177 this._registerKeyValue(this.key
, this.value
);
180 _unregisterKey: function(key
) {
181 this._unregister(key
, this._metaData
, this.list
);
184 _registerKeyValue: function(key
, value
) {
185 this._register(key
, value
, this._metaData
, this.list
);
188 _register: function(key
, value
, data
, list
) {
189 if (key
&& data
&& value
!== undefined) {
195 _unregister: function(key
, data
, list
) {
198 var value
= data
[key
];
200 this.arrayDelete(list
, value
);
208 `iron-meta-query` can be used to access infomation stored in `iron-meta`.
212 If I create an instance like this:
214 <iron-meta key="info" value="foo/bar"></iron-meta>
216 Note that value="foo/bar" is the metadata I've defined. I could define more
217 attributes or use child nodes to define additional metadata.
219 Now I can access that element (and it's metadata) from any `iron-meta-query` instance:
221 var value = new Polymer.IronMetaQuery({key: 'info'}).value;
223 @group Polymer Iron Elements
224 @element iron-meta-query
226 Polymer
.IronMetaQuery
= Polymer({
228 is
: 'iron-meta-query',
233 * The type of meta-data. All meta-data of the same type is stored
239 observer
: '_typeChanged'
243 * Specifies a key to use for retrieving `value` from the `type`
248 observer
: '_keyChanged'
252 * The meta-data to store or retrieve.
261 * Array of all meta-data values for the given type.
271 * Actually a factory method, not a true constructor. Only runs if
272 * someone invokes it directly (via `new Polymer.IronMeta()`);
274 factoryImpl: function(config
) {
276 for (var n
in config
) {
287 created: function() {
288 // TODO(sjmiles): good for debugging?
289 this._metaDatas
= metaDatas
;
290 this._metaArrays
= metaArrays
;
293 _keyChanged: function(key
) {
294 this._setValue(this._metaData
&& this._metaData
[key
]);
297 _typeChanged: function(type
) {
298 this._metaData
= metaDatas
[type
];
299 this.list
= metaArrays
[type
];
301 this._keyChanged(this.key
);
306 * Retrieves meta data value by key.
307 * @param {string} key The key of the meta-data to be returned.
310 byKey: function(key
) {
311 return this._metaData
&& this._metaData
[key
];