简体   繁体   中英

Calling controller action from action in component

I'm having some issues getting this to work. I thought I had done it correctly but it seems that's not the case.

index.hbs

{{#if image}}
    {{image-cropper imageToCrop=image setCroppedImage=setCroppedImage}}
    <br>
    {{/if}}
{{file-input action="setImage"}}

The file-input component takes an image in, and sets the image property to a data URL, which then causes the image-cropper component to show up.

image-cropper.hbs

<div class="cropper-container">
    <img src="{{imageToCrop}}">
</div>
<button {{action 'cropImage'}}>Crop Image</button>
{{croppedImage}}

When I click the button it calls the cropImage action in the image-cropper.js file

image-cropper.js

actions: {
    cropImage: function() {
        var container = this.$(this.get('cropperContainer'));
        var croppedImage = container.cropper('getCroppedCanvas');
        this.sendAction('setCroppedImage', croppedImage);
    }
}

So here I am using this.sendAction(); to call the setCroppedImage action name on the component, which refers back to the action name on the component in the index.hbs file, which I thought should call the action name on the controller to the left of the = sign

index.js(controller)

setCroppedImage: function(croppedImage) {
    console.log('set cropped image called on index controller');
    this.set('finalCroppedImage', croppedImage);
},

But the console.log statement is not being called so I know the controller action is not being called either.

I need some help understanding how to get this to work here.

After more reading I see that I need to add "" to the action name like so

From

{{image-cropper imageToCrop=image setCroppedImage=setCroppedImage}}

To

{{image-cropper imageToCrop=image setCroppedImage="setCroppedImage"}}

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