Ember component send action to route

For short answer you can you can use ember-route-action-helper addon.

<button {{action (route-action 'onButtonClick')}}>ClickToCallRouteAction</button>

There are three way of actions communication,

1. Old style classic functions style ie., passing function name as string from top to bottom. and in all the places we need to define same function and provide. Use sendAction to bubble. and send method bubble from controller to route hierarchy.

This is not encouraged.
Sample classic style actions twiddle

2. Closure actions
Use action helper pass function instead of just string. so that you don’t need to define it everywhere.
sample twiddle for closure actions style

3. route-action-helper addon
You can directly call route action from anywhere literally by just wrapping functions using route-action helper.

Sample twiddle

Comparision between Classic style and Closure style and Why Closure is preferrable ?

  • In classic style, You need to define actions at each level and use sendAction to trigger the action at each level until you got all the way out of your nesting.
  • You can return value in closure actions but not in classic actions.
  • You can curry values in closure actions but not in classic actions.
  • Closure actions fail immediately if the action is not found. but classic actions by design,would lazily raise errors only upon invocation
    values.
  • Coding complexity like who will handle actions and do business logic?.
  • In closure, you can combine action and mut helper to set a property with value. onclick=(action (mut title) value="titlevalue")
  • In closure, you can specify target object to invoke function. (action 'save' target=session) would look at the actions hash on the session object instead of the current context.

Some of the promising article regarding this,
– miguelcamba article ember-closure-actions-in-depth
– emberigniter article send-closure-actions-up-data-owner
– emberjs blog 1.13 release article
– dockyard – ember-best-practice-stop-bubbling-and-use-closure-actions
– blog from Ember map Why action helper?
– blog from Alisdair McDiarmid ember-closure-actions-have-return-values
– blog from alexdiliberto ember-closure-actions

Leave a Comment