Skip to content

Instantly share code, notes, and snippets.

@lukecoy
Last active January 8, 2019 19:51
Show Gist options
  • Save lukecoy/23816c076c5f04c5f2df8793206d0820 to your computer and use it in GitHub Desktop.
Save lukecoy/23816c076c5f04c5f2df8793206d0820 to your computer and use it in GitHub Desktop.
toggle-form example
import Ember from 'ember';
export default Ember.Component.extend({
isEditingEnabled: false,
// This gets passed in by the consumer of this
// component
onSubmit: null,
actions: {
onSubmit(event) {
event.preventDefault();
this.get('onSubmit')();
// This is closing the form after successful submission
this.set('isEditingEnabled', false);
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
inputText: '',
init() {
this._super(...arguments);
// simulate an api call
this.set('text', 'API value that was returned for example 1');
this.set('boolean', true);
},
actions: {
saveData(event) {
console.log('we saved something: ' + this.get('inputText'));
this.set('text', this.get('inputText'));
}
}
});
<h3>This is a example of a generic "toggleable form" component that handles text input</h3>
{{#toggle-form
text=text
onSubmit=(action "saveData")
}}
<input type="text" placeholder="Please enter stuff" required maxlength="3" oninput={{action (mut inputText) value="target.value"}}>
{{/toggle-form}}
{{#if isEditingEnabled}}
<form onsubmit={{action "onSubmit"}}>
{{yield}} <button onclick={{action (mut isEditingEnabled) false}}>Cancel</button><button type="submit">Confirm</button>
</form>
{{else}}
{{text}} <button onclick={{action (mut isEditingEnabled) true}}>Edit</button>
{{/if}}
{
"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