Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thoov/ember-invoke
EmberJS helper to invoke methods on a given context such as components or controllers.
https://github.com/thoov/ember-invoke
ember-addon emberjs function handlebars invoke method
Last synced: 28 days ago
JSON representation
EmberJS helper to invoke methods on a given context such as components or controllers.
- Host: GitHub
- URL: https://github.com/thoov/ember-invoke
- Owner: thoov
- License: mit
- Created: 2017-10-12T08:15:05.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-30T04:04:48.000Z (almost 7 years ago)
- Last Synced: 2024-10-15T06:43:28.034Z (30 days ago)
- Topics: ember-addon, emberjs, function, handlebars, invoke, method
- Language: JavaScript
- Homepage:
- Size: 62.5 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Ember Invoke
EmberJS helper to invoke methods on a given context such as components or controllers.
## Installation
`ember install ember-invoke`
## Usage
```hbs
{{invoke this 'methodName' 'any' 'number' 'of' 'args'}}{{!-- OR --}}
{{some-component prop=(invoke this 'methodName' 'any' 'number' 'of' 'args')}}
```## Why?
```js
// component/restaurant.js
import Component from '@ember/component';export default Component.extend({
people: [{
first: 'Bob',
last: 'Smith',
age: 43
}, {
first: 'Jen',
last: 'Doe',
age: 19
}, {
first: 'Bill',
last: 'Foobar'
age: 74
}],canDrink(age) {
return age >= 21;
}
})
``````hbs
{{!-- templates/component/restaurant.hbs --}}
{{#each people as |person|}}
{{person.first}} {{person.last}}
{{!--
The only way you could make this work would be to create a
`can-drink` helper that you would call like so:(can-drink person.age)
However, that logic might not make sense outside of the scope of
this component. Also, many times the logic is simple (like in this
example) and creating a whole separate helper file is over kill.
Also note that a computed property wouldn't work as we are within an
iteration and cannot create a cp that is bound to an individual item.
Instead we would have to create a wrapper cp that adds on these extra
properties which may or may not be what you want.This is where the invoke helper comes in. Now you can simply `invoke`
any method on a given context and it will be called with the arguments
that are passed in.
--}}{{#if (invoke this 'canDrink' person.age)}}
is of legal drinking age
{{else}}
is NOT of legal drinking age
{{/if}}
{{/each}}
```