Action composition for Ember.
ember install ember-compose-action-helper
Use the group-action
helper to combine multiple actions in to a single action.
For instance, if a form needs to be reset and an alert needs to be created after a form submission is successful:
Then in the controller the action handlers would look like:
import Ember from 'ember';
export default Ember.Controller.extend({
name: '',
actions: {
myAwesomeSubmit(name, afterSuccess, ev) {
ev.preventDefault();
this.store.createRecord('user', {name}).save()
.then(afterSuccess);
},
resetForm() {
this.set('name', '');
},
alertSuccess(user) {
window.alert(`The user ${user.name} was created`);
},
}
});
Function composition or piping can be performed using the compose
helper:
Then in the controller the action handlers would look like:
import Ember from 'ember';
export default Ember.Controller.extend({
name: '',
actions: {
add(a, b) {
return a + b;
},
multiply(a, b) {
return a * b;
},
alert(result) {
window.alert(`The answer is ${result}`);
},
}
});
This will alert "The answer is 42" when the button is clicked.