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

Custom tagName Components don't work with boolean HTML attributes #10976

Closed
owain opened this issue Apr 28, 2015 · 8 comments
Closed

Custom tagName Components don't work with boolean HTML attributes #10976

owain opened this issue Apr 28, 2015 · 8 comments

Comments

@owain
Copy link

owain commented Apr 28, 2015

HTML attributes on a custom element (Ember Component with custom tagName) don't work with attributeBindings as expected. When trying to bind the disabled state of a custom element the attribute is not set correctly according to it's boolean value.

Example

<button disabled={{isDisabled}}> will work as expected but {{custom-button disabled=isDisabled}} (with tagName: ‘custom-button’) won't work as it produces <custom-button disabled=“false”> (the presence of the disabled attribute means it’s disabled even though it's set to false).

So the following Ember Component:

App.CustomButtonComponent = Em.Component.extend({
  tagName: 'custom-button',
  attributeBindings:['disabled'],
  disabled: false
});

Output: <custom-button disabled="false">Press Me</custom-button>
Expected: <custom-button>Press Me</custom-button>


Here's a JSBin Example to illustrate the issue.

The behaviour seems to have changed in Ember 1.10/1.11. This JSBin shows the behaviour in Ember 1.9.1 where the disabled attribute never gets set to true. But I'm sure this used to work in our app before we upgrade so I'm a bit confused.

I chatted a bit with @mmun and it's likely because Ember uses HTMLUnknownElement for custom elements and attrName in elementInstance to check if its a DOM property or not.

@mmun
Copy link
Member

mmun commented Apr 28, 2015

I'm marking this as a regression for now, but the behaviour of custom elements was never specified.

@owain
Copy link
Author

owain commented Apr 28, 2015

Mentioning #9595 as it looks related.
@rwjblue suggested using null for attributes that should not be added - but this wan't in the context of a custom tag name.

@Truffula
Copy link

I'll throw this in here in case it helps someone in the meantime:

// There are two hard things in computer science...
Ember.computed.nullIfEmptyOrFalse = function(name) {
    return Ember.computed(name, {
        get: function() {
            var val = Ember.get(this, name);
            return !val || Ember.isEmpty(val) ?
                null :
                val;
        }
    });
};

@webark
Copy link
Contributor

webark commented Jul 29, 2015

in ember 1.13.3 (cli 1.13.1), <button disabled="{{false}}"> Button </button> is redering <button disabled> Button </button> for me.

@mmun
Copy link
Member

mmun commented Jul 29, 2015

Use disabled={{false}}.

@webark
Copy link
Contributor

webark commented Jul 29, 2015

ooohhh... 🐑.... (force wave) "I was never here"

@mmun
Copy link
Member

mmun commented Jul 29, 2015

Did you guys hear something?

@rwjblue
Copy link
Member

rwjblue commented Aug 19, 2015

I believe this is working as intended currently. Using null or undefined prevents setting an attribute (similar to how {{bind-attr}} worked previously), but using false is coerced to a string which means true.

Demo using null: http://emberjs.jsbin.com/heqecu/edit?html,js,output

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

No branches or pull requests

5 participants