2

I'm learning Ember and got into an issue I can't seem to find a solution for: I got a view that contains a component and that component handles a button click. I want to "send" that action to the wrapping view and handle it there if possible. That'd be the preferred way. If not possible, then handling it in the view's controller is OK too.

But I can't seem to catch that action anywhere, not even in the current route or the route's controller. I don't know where the action is being sent to, I thought I'd be able to handle it in the current context's controller but it seems that is not the case.

Here is a fiddle that illustrates my issue: http://emberjs.jsbin.com/juzif/1/edit

I get the action inside the component, but the sendAction doesn't seem to do much.

What am I doing wrong?

Edy Bourne
  • 5,679
  • 13
  • 53
  • 101

2 Answers2

1

I still haven't seen any solution of how to target the view from the component, but for the controller->route etc you need only define where the action need be sent to from the component (this is super helpful when you have the component in a particular controller/routes scope and you want it to be handled differently based on the interaction of each component, or if you want to ignore it, which is currently what's happening for you)

{{foo-bar blah=view.foo dataUpdated='dataUpdated'}}

or better said

{{foo-bar blah=view.foo internalAction='externalAction'}}

http://emberjs.jsbin.com/misiyaki/1/edit

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • Thanks again!! Worked like a charm. I'll look into Ember source and see if I can find a way to target the view, and if not maybe I can submit a pull request for it. – Edy Bourne Jun 04 '14 at 21:05
  • I'd imagine defining nothing would be equivalent to `{{foo-bar blah=view.foo dataUpdated='dataUpdated'}}`, where internal and external action names are the same, though. Seems unnecessary having to define it explicitly when both have the same name. Am I missing something? – Edy Bourne Jun 04 '14 at 21:21
  • 1
    the concept with reusable components is that you ignore actions unless you explicitly subscribe to them, that way someone can define a million actions you could subscribe to on a component, and if you only wanted to capture one of them, you wouldn't need to define each action in your controller/route just to catch all of the sent actions. (as you may or may not know, Ember yells at you when you send an action and nobody received it, which is pretty convenient for knowing when an action is ignored accidentally) – Kingpin2k Jun 04 '14 at 21:27
  • hummm I see. I assumed you would chose which actions to subscribe by implementing a method to catch them, and ignore by not implementing any. This approach works too, but its more verbose. Anyways, thanks for the explanation! – Edy Bourne Jun 04 '14 at 21:30
0

Additional answer responding to "I still haven't seen any solution of how to target the view from the component":

The magic word here is targetObject:

{{foo-bar action='actionName' targetObject=view}}

This also works in each loops targeting an itemController like so:

{{#echo posts itemController="post" as |p|}} 
   {{foo-bar action='actionName' targetObject=p }} 
{{/each}}

taken from @jasonmit´s answer here: How to send an action from a Ember component to a View that wraps it?

Community
  • 1
  • 1
Jeff
  • 6,895
  • 1
  • 15
  • 33