I need to combine linkTo and action helpers in Ember.js. My code is:
JavaScript
x
2
1
{{#link-to 'index'}}<span {{action 'clear'}}>Clear</span>{{/link-to}}
2
But I would like to make this something like this:
JavaScript
1
2
1
{{#link-to 'index' {{action 'clear'}} }}Clear{{/link-to}}
2
And also:
JavaScript
1
6
1
<li>
2
{{#link-to 'support'}}
3
<span {{action 'myAction' 'support'}}>Support</span>
4
{{/link-to}}
5
</li>
6
To:
JavaScript
1
4
1
<li>
2
{{#link-to 'support' {{action 'myAction' 'support'}} }}Support{{/link-to}}
3
</li>
4
How can I achieve this?
Solution
Check my answer for Ember 2.0 compatible, OK for SEO solution.
Advertisement
Answer
Ember Link Action addon
This is OK for SEO solution!
Install addon
JavaScript
1
2
1
ember install ember-link-action
2
Usage
You can pass closure action as invokeAction
param to {{link-to}}
component:
JavaScript
1
4
1
{{#link-to 'other-route' invokeAction=(action 'testAction')}}
2
Link to another route
3
{{/link-to}}
4
To pass parameters to action you can use:
JavaScript
1
4
1
{{#link-to 'other-route' invokeAction=(action 'testAction' param1 param2)}}
2
Link to another route
3
{{/link-to}}
4
Compatibility
Automated test suite confirms that addon works with 1.13 up to latest Ember 3 releases.
It works with a release, beta and canary versions of Ember.
Addon GitHub repository. Contributions are welcome.