2

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 :) ?

Parijat Kalia
  • 4,929
  • 10
  • 50
  • 77

1 Answers1

3

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

Luke Melia
  • 8,389
  • 33
  • 41
  • yea I totally do, the buttons exist outside of the view. But they manipulate this specific view, trying to have them within this view itself challenges my semantic sensibilities – Parijat Kalia Dec 19 '13 at 20:35
  • If the button exists outside the view, have it send an action up to a controller that the views share access to. The action handled in the controller would update a property that the view would then respond to accordingly. e.g. http://emberjs.jsbin.com/aGagORaz/2/edit?html,js,output – Luke Melia Dec 23 '13 at 03:25
  • you feel that is a better strategy than simply having the button specify which view it is supposed to manipulate? It feels a little bit of an overkill sending an action request to a controller when it is really just a pure view response. – Parijat Kalia Dec 27 '13 at 19:55
  • IMO, controllers don't know anything about views, so sending an action to be handled by the controller which in turn passes on the action to one of it's views is just counter intuitive. Also, your solution works fine if one is a childView of the other, but not if the aren't – Parijat Kalia Dec 27 '13 at 20:08
  • just figured this, I did this view.parentView.targetView in my button action target attribute. Worked :) – Parijat Kalia Dec 27 '13 at 20:52
  • Sending up to a controller and letting the result of a property change flows down means that your views remain decoupled from each other, and in particular, view hierarchy can be changed without a problem. Just cleaner and more maintainable, IMO. – Luke Melia Dec 31 '13 at 18:38
  • Oh yes, definitely agreeing with that. – Parijat Kalia Jan 01 '14 at 00:19