Skip to content

Instantly share code, notes, and snippets.

@mrosata
Last active June 26, 2016 13:20
Show Gist options
  • Select an option

  • Save mrosata/78532bea9f9e51f647e5cd0b64da749c to your computer and use it in GitHub Desktop.

Select an option

Save mrosata/78532bea9f9e51f647e5cd0b64da749c to your computer and use it in GitHub Desktop.
Disabled Button Example
import Ember from 'ember';
export default Ember.Controller.extend({
isDisabled: false,
pureBoolean: Em.computed('isDisabled', function() {
return !this.get('isDisabled');
}),
actions: {
makeDisabled() {
this.set('isDisabled', true);
},
// this resets example
resetExample() {
this.set('isDisabled', false);
}
}
});
<h1>Disabled Button Example</h1>
<p>
Use of truthy and falsey on a disabled button and also use of actual strict boolean on a disabled button (the reset button).
</p>
<button disabled={{if isDisabled true null}} {{action 'makeDisabled' on='click'}}>Click to disabled</button>
<button disabled={{pureBoolean}} {{action 'resetExample'}}>Reset Button</button>
{{outlet}}
<br>
<br>
{
"version": "0.9.3",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.4.5",
"ember-data": "2.1.0",
"ember-template-compiler": "2.4.5"
}
}
@mrosata

mrosata commented Jun 22, 2016

Copy link
Copy Markdown
Author

This was a Twiddle done to demo an answer that was on StackOverflow. It is a workaround to returning null from a JavaScript function just to be able to make a disabled property function correctly in a template. It's a small thing but it separates concerns because now the JavaScript can return any falsey value and get the same effect as returning null to the property used to determine the buttons disabled state.

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