A slightly more idiomatic way to invoke actions in your Ember components
npm install ember-invoke-actionA slightly more idiomatic way to invoke actions in your Ember components.
```
ember install ember-invoke-action
You can either use ember-invoke-action as a helper function or a mixin.
`javascript
import Ember from 'ember';
import { InvokeActionMixin } from 'ember-invoke-action';
export default Ember.Component.extend(InvokeActionMixin, {
click(...args) {
this.invokeAction('click', ...args);
}
});
`
`javascript
import Ember from 'ember';
import { invokeAction } from 'ember-invoke-action';
export default Ember.Component.extend({
click(...args) {
invokeAction(this, 'click', ...args);
}
});
`
As alternative to invokeAction you can call strictInvokeAction.strictInvokeAction is functionally the same as invokeAction except for whenstrictInvokeAction
the given action could not be found, then will raise anAssertionError.
With the invoke helper you can call other actions from the actions object as
if it is a closure action.
`javascript
import Ember from 'ember';
import { invoke } from 'ember-invoke-action';
export default Ember.Component.extend({
actions: {
saveModel() {
return get(this, 'model').save();
},
closeModal() {
set(this, 'modalVisible', false);
},
saveModelAndClose(...args) {
invoke(this, 'closeModal');
return invoke(this, 'saveModel');
}
}
});
`
* npm run lint:jsnpm run lint:js -- --fix
*
* ember test – Runs the test suite on the current Ember versionember test --server
* – Runs the test suite in "watch mode"ember try:each` – Runs the test suite against multiple Ember versions
*
This code was inspired by @miguelcobain, I just made an addon out of it.
License
------------------------------------------------------------------------------
This project is licensed under the MIT License.