Ember.Component sendAction () to view the target

sendAction()in Ember.Component bubbles for the default controller that is expected. But I have 2,3 actions that I need to send to the corresponding view that the component uses. In templates, we set the action to view with target=view. Can we do this?

Update . Currently, when I work, I am sending a view object to a component, which from there invokes an view.send()action to send. But I feel that this is not right.

+4
source share
2 answers

Well, after some thought, I believe that I know what you mean. If you have a component and you have an action, it will be handled by the component itself. If you want to send an action outside the component, you must use sendAction.

Now, to target the view in which your component is stored, since your component is the base of the view, you can probably do this.get('parentView')it to get the parent view, and then chainsend('nameOfAction')

Thus, it will be this.get('parentView').send('nameOfAction')from within the component's action, and it then triggers the action on the parent view from which the component is embedded.

So, in your component, you could:

 App.DemoCompComponent = Ember.Component.extend({
   actions: {
     internalTrigger: function() {
       //just an alert to make sure it fired
       alert('Internal action was caught on component');
       //this should target parent view component is in
       this.get('parentView').send('viewTriggerTest');
    }
  } 
 });

Now let's say that you have a component in the index template that you could:

The template will look like this:

   <script type="text/x-handlebars" data-template-name="index">
     <h2>Inside Index Template</h2>
       {{demo-comp}}
   </script>

Index Pointer Code:

 App.IndexView = Ember.View.extend({
   actions: {
     viewTriggerTest: function() {
       alert('View Trigger Test Worked on Index!');
     }
   }
 });

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

+6

Ember 2.9 closure action . target parentView .

0

Source: https://habr.com/ru/post/1548633/


All Articles