Ember: Specify a target action view for an action helper, other than the containing or parent view

1.6k views Asked by At

In Ember, if you want an action to be handled by a view, it's easy to achieve this using

<button {{action 'something' target='view'}}>
`{{view App.targetView}}` // this is the view in which the 'something' action ought to be handled

If you wanted the parentView to handle this, then target='view' gets replaced with target='parentView'

Fair enough, my problem is that I want a childView to handle and simply stating 'childView' yields

'Cannot read property 'send' of undefined '

I could choose to specify the name of the view in here, like :

target='targetView'

where

{{view App.targetView viewName="targetView"}}

that did not work. Neither did

target ='views.targetView' nor 'childViews.targetView'

Please don't guide me to change the semantic structure of my app by stating that the button with this action needs to be within the App.targetView template because that to me is a hack and messing with the semantic structure of my app.

What I do need to know is how I can use the target property to specify a specific view, not just the containing or parent view :) ?

1

There are 1 answers

7
Luke Melia On BEST ANSWER

To be honest, my instinctive response is "eek, don't do that". But, I'm going to suspend disbelief and stipulate that you have a good reason for wanting to do this.

The reason your approach did not work is that the target property path is evaluated in the template's context (which by default is the controller). Solution: use the view keyword in the property path and chain the viewName of the target view.

e.g.

<button {{action "foo" target="view.targetView"}}>Click me</button>
{{view App.TargetView viewName="targetView"}}

http://emberjs.jsbin.com/aGagORaz/1/edit?html,js,output