K
K
Klaus Kater2016-09-28 23:01:38
Ember.js
Klaus Kater, 2016-09-28 23:01:38

How to handle event in child component, ember.js?

Hello.
There is a component inside which there are a bunch of others, an event can occur on this heap, and I want to handle this event with one, the most important component. Tell me how to do it? Those. it is necessary to launch an event from the child component in the parent, saving the information where the event came from.

export default Ember.Component.extend({
    actions: {
         open(obj){
           console.log('OPEN');
        },
});
export default Ember.Component.extend({ // дочерние компоненты
       alarm(){
           this.send('open', this);
       },
});

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andy Developer, 2016-09-29
@UmbrellaCoders

{{child-component open=(action "open")}}
like this?
emberjs.com/api/classes/Ember.Component.html#metho...

test(obj){
      console.log(obj);
    }

open(){
      console.log('child fired');
      this.sendAction('test', this);

    }

here, I just tested it myself, ember 2.7

S
Stanislav Romanov, 2016-09-29
@Kaer_Morchen

export default Ember.Component.extend({
    actions: {
         open(obj){
           console.log('OPEN');
        },
    },
});

export default Ember.Component.extend({ // дочерние компоненты
       alarm(){
           this.sendAction('alarm', this);
       },
});

/app/templates/components/parent-component.hbs
{{#parent-component as |parent|}}
    {{child-component alarm=(action "open" target=parent)}}
{{/parent-component}}

K
Klaus Kater, 2016-09-29
@kurojneko

And there are no options not to explicitly pull the event through the components, if there is a large tree of them, each one must be called with the transfer of an action ...
I thought to do it through the service, but you can transfer the event to the service, but from the service - again a problem.
in general, while I've done a crutch, it's a shame, but it works.

export default Ember.Service.extend({
    stack_view:[],
    open(obj){
    },
});

export default Ember.Component.extend({
    manager: Ember.inject.service(),
    init:function(){
         this.get('manager').set('open', this.open);
    },
    actions: {
         open(obj){
           console.log('OPEN');
        },
});
export default Ember.Component.extend({ // дочерние компоненты
       manager: Ember.inject.service(),
       actions: {
        open(){
          this.get('manager').open(this);
        },
      }
});

V
Vitaly Pukhov, 2016-10-01
@iproger

had similar problems before, you need to update to the latest revisions, they no longer have these failures

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question