ember.js icon indicating copy to clipboard operation
ember.js copied to clipboard

No way of binding nested component actions programmatically?

Open orochi235 opened this issue 12 years ago • 10 comments

Is there a clean/public way to dynamically bind the actions of two nested Ember components in JS? I've spent a fair amount of time digging through docs and Stack Exchange posts, and it seems like there's no way to do this via Ember's public API. Is this an overlooked feature, an architectural constraint, or a deliberate design decision?

Some background: We've created a number of Ember components that dynamically instantiate each other. For instance, we have two called "dropdown-list" and "choice-list". When clicked, dropdown-list instantiates and displays a choice-list, and receives an action when an item in the choice-list is selected.

This is obviously easy to do in a Handlebars template: you just declare the component and map the child's action to the name of the parent's action handler. But this isn't really an option for us, largely because the child component's DOM structure isn't a subtree of the parent component's DOM tree (it's being displayed in a global modal layer instead).

I'm basically looking for a JS equivalent for the second half of this Handlebars declaration:

{{choice-list userSelect='handleUserSelection'}}

If it helps, here's how we're dynamically instantiating child components (from within an action handler on the parent component):

this.set('list', App.ChoiceListComponent.createWithMixins(App.ModalMixin, {
    /* ... */
}));

orochi235 avatar May 13 '14 13:05 orochi235

This is a very common problem and something I'd love to have a solution for.

tomdale avatar May 13 '14 22:05 tomdale

Thanks for responding. A coworker pointed out that setting childAction: 'parentAction' on the child component in JS actually does the job, and I sheepishly closed the issue. Is this a kosher way of achieving this, or is there additional magic going on when it's declared in Handlebars?

orochi235 avatar May 14 '14 00:05 orochi235

@tomdale have we made any progress here?

wagenet avatar Nov 01 '14 19:11 wagenet

sorry about the missed post pressed, anyway…

hi, i got similar issue when i'm trying to make some kind of smart forms. it's not exactly the same problem but i wish i could find a way to catch actions with components (maybe there is but i can't find?).

the idea is that component <form-top> will catch action from <form-bottom> without having the controller as a mediator.

{{#form-top}}
    {{#form-bottom actionChange="actionChange"}}Will fire actionChange on some event{{/form-bottom}}
{{/form-top}}

if there was for example an option for example to declare on <form-top> actions that it catches. for example:

{{#form-top actions="actionChange"}}
    {{#form-bottom actionChange="actionChange"}}Will fire actionChange on some event{{/form-bottom}}
{{/form-top}}

it still doesn't solve all the problem, because i will still have to have a property in the controller that will be binded to both component to connect. but it at least can make the controller less bloated.

oriSomething avatar Jan 11 '15 09:01 oriSomething

I am running into the same issue. I have a component, say foo. In one place in my application, I create it using handlebars {{foo someAction='doSomething'}}.

In another place, I create the component programmatically:

Foo.create({someAction: 'doSomething'});

Inside the foo component's click, I call this.sendAction('someAction');

For the component created via handlebars, everything works. For the component created programmatically, the actions never occur.

Is this the correct way to pass an action when programmatically creating a component, or should I be doing something else?

mikeazo avatar Mar 10 '15 14:03 mikeazo

I think you can get around this by setting the targetObject property of the “sending” component to reference the “receiving” component. Not sure how kosher of a solution this is, but it’s worked for me in the past.

On Mar 10, 2015, at 10:13 AM, mikeazo [email protected] wrote:

I am running into the same issue. I have a component, say foo. In one place in my application, I create it using handlebars {{foo someAction='doSomething'}}. That works fine.

In another place, I create the component programmatically:

Foo.create({someAction: 'doSomething'}); Inside the foo component's click, I call this.sendAction('someAction');

For the component created via handlebars, everything works. For the component created programmatically, the actions never occur.

— Reply to this email directly or view it on GitHub https://github.com/emberjs/ember.js/issues/4859#issuecomment-78060823.

orochi235 avatar Mar 10 '15 15:03 orochi235

@orochi235 You are correct, this does fix it. If I print the targetObject for the handlebars created component, it's targetObject is set. If I print it for the programmatically created one, it is null. Once I set it manually, everything worked.

mikeazo avatar Mar 10 '15 18:03 mikeazo

{{#form-top actions="actionChange"}} {{#form-bottom actionChange="actionChange"}}Will fire actionChange on some event{{/form-bottom}} {{/form-top}}

the way context shifting works now, this isn't a problem anymore, yielded block actions bubble up lexically

stefanpenner avatar Mar 10 '15 18:03 stefanpenner

Foo.create({someAction: 'doSomething'});

passing these to create is also legit.

Will reopen, if some other example can be provided that is a pain.

stefanpenner avatar Mar 10 '15 18:03 stefanpenner

@stefanpenner so are you saying that in newer versions of ember I don't have to set the targetObject? I'm on 1.8.1 right now.

mikeazo avatar Mar 10 '15 18:03 mikeazo