Hook into controller formAfterSave and eventually show modal window

Is there an easier way to hook into controller formAfterSave and eventually show modal window, than following?

In my controller I have:

public function formAfterSave($model)
{
        if ($something === true) {
            $this->addJs('/plugins/acme/plugin_name/controllers/assets/js/script.js');
        }
}

And then in my controllers update.htm I have my modal markup just at the end of everything else:

<div class="control-popup modal fade" id="my-modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    Hello
                </h5>
                <button type="button" class="btn-close" data-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>
                    Message...
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">
                    Close
                </button>
            </div>
        </div>
    </div>
</div>

Then in my script.js I just have:

if ($('#my-modal').length) {
    $('#my-modal').modal('show');
}

Hey @maki3000

This looks like a good use case for dispatching a browser event:

Here is an example that listens for the app:password-confirming event and displays a popup:

Triggered by

$this->dispatchBrowserEvent('app:password-confirming');
1 Like