Polymer global variables document may be wrong~
-
21-12-2019 - |
Question
I'm new to polymer and now reading the document. But I'm confused about the following document:
(function() {
var values = {};
Polymer('app-globals', {
ready: function() {
for (var i = 0; i < this.attributes.length; ++i) {
var attr = this.attributes[i];
values[attr.nodeName] = attr.nodeValue;
}
}
});
})();
and then define the global variables like this:
<app-globals firstName="Addy" lastName="Osmani"></app-globals>
I've tried this way but I can't get any variables via app-globals, the Value
is absolutely a local variable for it is not with a this.
started, then how can I get the value via app-globals?
Solution
This looks like a mistake in the documentation. I believe the intent is something like this:
<polymer-element name="app-globals" attributes="values">
<script>
(function() {
var values = {};
Polymer('app-globals', {
ready: function() {
// this bit at least is missing from the example
this.values = values;
// initialize values from attributes (note: strings only)
for (var i = 0; i < this.attributes.length; ++i) {
var attr = this.attributes[i];
values[attr.nodeName] = attr.nodeValue;
}
}
});
})();
</script>
</polymer-element>
This way the data is available from any instance of app-globals
as instance.values
. I also published values
so that you can bind to it.
... in some element template ...
<app-globals values="{{globals}}"></app-globals>
<h2>{{globals.header}}</h2>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow