The document of global variable polymers may be wrong ~

I am new to polymer and am now reading a document. But I am 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 global variables as follows:

<app-globals firstName="Addy" lastName="Osmani"></app-globals>

I tried this way, but I can not get any variables through app-globals, Valuethis is an absolutely local variable, because it is not with a start this., then how can I get the value through the Global application?

+4
source share
1 answer

It looks like a documentation error. I believe the intention 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>

Thus, data is accessible from any instance app-globalsas instance.values. I also posted valuesso you can get attached to it.

... in some element template ...

<app-globals values="{{globals}}"></app-globals>
<h2>{{globals.header}}</h2>
+3

All Articles