-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Can't declare Boolean attributes with default of true? #1812
Comments
+1 to being able to use One more thing. It also causes more confusion when I use <template is="dom-repeat" items="{{arr}}">
<x-test bool-attr="{{item.foo}}"></x-test>
</template> If Polymer were strict about "Boolean properties set based on the existence of the attribute", then there is no way to set |
Note that this isn't really Polymer's definition of a boolean attribute, it's HTML's definition of a boolean attribute: http://www.w3.org/TR/2008/WD-html5-20080610/semantics.html#boolean I think there's a tension here between something that's arguably more flexible (foo="false") and something that's arguably more idiomatic with native elements (presence == true). Even the web platform is incoherent here, since ARIA uses string values for "true" and "false". @punzki you're confusing setting an HTML attribute and data binding to a property. The value of |
@arthurevans - That's precisely my point, that it is confusing, and inconsistent. When you put |
I'm curious to better understand the use-case for having a boolean property have a default of true. Would it not be possible to simply reverse the logic behind the property such that the desired functionality would be possible with a default of false? For example, assuming we're working with an element that validates villains to see whether or not they're fit of being someone's arch-nemesis, instead of using Basically, what I'm getting at is: what's an expectable use-case where this is not possible? |
@vsimonian There are cases where the most natural and desirable attribute name would be framed in the positive. While attributes are just symbols, and their meaning can be inverted and still make logical sense, an API is tantamount to language, and humans want language to read naturally. In some cases, the positive form of a word may be more concise or common than the negative form. (Some rare words like "inert" have no opposite form.) In other cases, the negative form may be misleading or inaccurate. And it's often desired that an API should mirror the corresponding terms in the user interface.
|
I encountered this when migrating elements from 0.5 to 1.0. To avoid coming up with corresponding negative forms, I just used an additional word in front of the attribute names to represent an attribute is disabled or its effects have been inhibited or its dimensions have been reduced (something like Although more than one word, this seemed to:
The prefix |
As Arthur mentioned, Boolean attributes have a specific platform meaning from which we do not want to stray. We also feel that the practice of defaulting booleans to false makes sense in the vast majority of cases, and places where the platform has strayed (e.g. That said, there are some options if you choose to go forward implementing this type of API for your elements.
|
@kevinpschaaf I think Polymer's position is reasonable. Thanks for letting us know about those options! |
Polymer properties with default value = true have to use Type: Object, otherwise it will not work. See also: Polymer/polymer#1812 (comment)
This is a shame. It is not intuitive at all. I need to programatically set the value for paper-input-container's noLabelFloat boolean property It's wrapped in a custome element and I need to disable noLableFloat in some cases.. It cannot be done via a binding thanks to this behaviour. I have to resort to dom-if logic, or write code that sets the property value manually. All that could be avoided if I could just bind a value. |
This isn't related to data binding. This only affects configuring the element from static markup. You can easily use a data binding to toggle |
Many thanks. I got it working with the binding. In my state of confusion when I first countered this I issue I messed up my binding declaration and concluded incorrectly that it didn't work. |
Glad to hear it 👍 |
because HTML attribute can't have true default value. Polymer/polymer#1812
…1812 Change: 151755389
…bahi#15 Component attributes are "true" if set and "false" if not. Therefore, default values of "true" can never be set to "false" through attributes. Explained more in detail here: Polymer/polymer#1812 (comment)
The way attributes are deserialized seems to preclude the declaration of attributes that default to true.
From the docs: "Boolean properties set based on the existence of the attribute: if the attribute exists at all, its value is true, regardless of its string-value (and the value is only false if the attribute does not exist)."
That works for attributes with a default value of false. If someone wants the default value, they leave the attribute off. If they want the non-default value of true, they can explicitly say attr="true" (or any string).
But I don't see how to declare support for a Boolean attribute that defaults to true. See http://jsbin.com/neyaxu/1/edit?html,output. Here, I want to be able to override the default behavior by saying red="false". Since Polymer interprets any string value of true, however, it doesn't appear to be possible to declaratively set the attribute to false. That can be done imperatively, but it should also be possible imperatively.
While an attribute's logic can always be flipped such that its default value is false, there are cases where an attribute's obviously API name naturally suggests a default value of true.
The text was updated successfully, but these errors were encountered: