Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.9] hostAttributes: Noooooooo! #1549

Closed
gertcuykens opened this issue May 16, 2015 · 5 comments
Closed

[0.9] hostAttributes: Noooooooo! #1549

gertcuykens opened this issue May 16, 2015 · 5 comments

Comments

@gertcuykens
Copy link

Please just stick with using properties for everything

  properties: {
    checked: {
      type: bool,
      default: true,
      //indicate private or public here if you must
    }
  }

Properties are properties don't confuse everybody with the following

hostAttributes: {
  checked: true
}
@ebidel
Copy link
Contributor

ebidel commented May 16, 2015

Attributes and properties are subtle in native elements. The important bit is that attributes do not always map to properties (Aria attrs) and vice versa (HTMLSelectElement.selectedIndex does not have an attribute). hostAttributes allows you to specify default attributes on element instances without defining a property, accessible in JS.

A good example for needing hostAttributes is aria:

hostAttributes: {
  role: 'link'
}

When created: <my-link role="link">. Users could instead use setAttribute('role', 'link') if they don't want to use hostAttributes, but the latter is a bit of sugaring.

If you want a property that also reflects to an attribute, use reflectToAttribute: true.

@gertcuykens
Copy link
Author

Ok thanks for the explanation, I can see now why it must be.

@zerodevx
Copy link

Thanks ebidel for the concise explanation - should definitely be added to the docs.

I was confused too when clearly I could define exposed attributes (ie the public api) via the polymer element properties object, but your explanation somewhat clears up the difference between user-created properties and default HTML attributes.

@marcus7777
Copy link

so do I do both

...
hostAttributes: {
  role: 'link'
},
...

&

...
  properties: {
    role: {
      type: string,
      default: "link",
      reflectToAttribute: true
    }
  },
...

?

@ebidel
Copy link
Contributor

ebidel commented Jun 10, 2015

Should be just the first. Elements dont have a role property. The aria stuff is just attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants