简体   繁体   中英

How to send an action from a Ember component to a View that wraps it?

I know the sendAction will send actions from a component to the controller associated with the template where it has been placed, but in my case the component is not placed directly inside a route's template. Instead, my component is inside a view's template:

<script type="text/x-handlebars" data-template-name="displayTemplate">
    <h3>View</h3>

    ...

    {{componentA}}

</script>

This component has a controller associated with it:

App.ComponentAController = Ember.Controller.extend({
    ...
}

But the wrapping view does not, it's just a view:

App.DisplayView = Ember.View.extend({
    templateName: 'displayTemplate',

    actions: {
         updateData: function(data) {
             /* how can I get this triggered when an action happens in the embedded componentA? I'd like to 
             process the data object here so I can update the wrapping view accordingly. */
         }
    }
...
}

If I implement the action in the component's controller, it is not triggered when I perform a sendAction('updateData', data) from within the component. If I implement that in DisplayView , it is not triggered either.

So the question is: when an action is triggered in componentA , how can I send that action to the view DisplayView for handling?

More specifically, I'm trying to send context data to that view so it can be updated depending on what is selected on the embedded component. So if there is another way of communicating this data that'd work too. I chose actions just because it seemed appropriate, but maybe it is not.

UPDATE: The actual scenario

The actual code refers to a grid view, which in it's template has a pagination component. When the user switches to a new page, the pagination component sends a request to the server for the selected page.

Once data is returned, it then needs to let the wrapping view (which contains the grid) know that new data is available. When the user clicks on the page number, the action is handled in the pagination component and that's why I make the data call from there. If I needed to paginate something else, I wanted to reuse this component so I didn't want to make the pagination part of the grid view.

通常,您传递target ,但是组件上下文的属性名称为targetObject

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM