Dominic
Dominic

Reputation: 419

When do you need to pass the name of your element to the main Polymer function?

When defining custom elements, the API developer guide always passes the name of the custom element as the first argument to the Polymer() function.

Polymer('my-element', {/*stuff*/});

In the Tutorial, however, the Polymer() function is consistently called without the name and simply passed the prototype object.

Polymer({/*stuff*/});

As the Polymer() function doesn't seem to be explicitly defined anywhere in the documentation I was just wondering what the recommendation is. Are there any pitfalls to leaving the name out or should you always pass it just to be safe?

Upvotes: 1

Views: 57

Answers (2)

Scott Miles
Scott Miles

Reputation: 11027

You can omit the name argument to Polymer() iff:

  1. your definition is in an import
  2. the <script> node containing the Polymer() call is inside the matching <polymer-element> node

Upvotes: 3

Oliver
Oliver

Reputation: 978

In my opinion you should always declare the name of your polymer-element to make your code more readable, at least for others.

Nevertheless, it looks like you can skip the name and let Polymer do the detection. If I get it right Polymer looks for the name attribute of its first parent node.

https://github.com/Polymer/polymer-dev/blob/master/src/declaration/polymer.js#L20-29

I tried it on https://ele.io and it worked for me.

Upvotes: 2

Related Questions