Skip to content

Instantly share code, notes, and snippets.

@thoov
Created April 2, 2019 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoov/d24058c256a364d5b15f066e2129e687 to your computer and use it in GitHub Desktop.
Save thoov/d24058c256a364d5b15f066e2129e687 to your computer and use it in GitHub Desktop.
Disabled Button Example
import Ember from 'ember';
import { computed } from '@ember/object';
export default Ember.Component.extend({
childInputs: null,
init() {
this._super(...arguments);
this.childInputs = Ember.A([ { isValid: false }, { isValid: false } ]);
},
disableButton: computed('childInputs.@each.isValid', function() {
let inputs = this.childInputs;
let allInputTrue = inputs.every((i) => i.isValid === true);
return !allInputTrue;
}),
actions: {
validityChanged(idx, value) {
this.get('childInputs').replace(idx, 1, Ember.A([ {isValid: value }]));
}
}
});
import Ember from 'ember';
import { computed } from '@ember/object';
export default Ember.Component.extend({
inputValue: null,
didInsertElement() {
this._super(...arguments);
this.validityChanged(this.isValid);
},
isValid: computed('inputValue', function() {
return parseInt(this.inputValue, 10) > 10;
})
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<MyForm as |form|>
<form.inputFoo />
<form.inputBar />
</MyForm>
<h1>My Form</h1>
<form>
{{yield (hash
inputFoo=(component 'my-input' validityChanged=(action "validityChanged" 0))
inputBar=(component 'my-input' validityChanged=(action "validityChanged" 1))
)}}
<br>
<button disabled={{disableButton}}>Submit</button>
</form>
{{input value=inputValue placeholder="Enter a value > 10" key-up=(action validityChanged isValid)}} Is Valid: {{isValid}}
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment