I want to load knockout content in modal popup on the cart page.
And to be able to bind button/click events.
Best Answer
Yeah, this was hard to figure out.
I could not find any example of this in Magento docs.
I wanted to be able to bind button/click events and data to the popup, so I could call methods in my component based on user input in those popups.
Just putting a click bind on the model dom did not fire as when Magento model copies the content it does not have knockout bindings applied!
First your js needs to define the appropriate things like Magento_UI/js/modal/modal
onRenderComplete: function () {
var viewModel = this;
var popupOptions = {
type: 'popup',
responsive: true,
innerScroll: true,
modalClass: 'my-popup',
title: '<b>Save Thing</b>',
'buttons': [{
text: 'Cancel',
class: 'action'
}],
opened: function () {
// Because magento modal copies the dom... we need to apply bindings. But it only copies once. So we want to only apply the bindings on first open.
if (this.appliedBindings === undefined) {
ko.applyBindings(viewModel, this);
this.appliedBindings = true;
}
}
};
In our content.html we want to add a popup such as.
I couldn't get an override of the modal.closeModal() function to work via mixins and also I think doing it via a mixin makes it override through the whole website on all modals, which I do not want. I only need it on this one modal.
I ended up by simply removing any trigger that will call modal.closeModal(). There are some other modal options you can use to achieve that:
I hide the close button on opening the modal with the opened option/event which will be triggered right after the modal has been opened
I'm overriding the keyEventHandlers.escapeKey option
You simply have to listen for the 'modalclosed' event on the DOM element on which the modal had been invoked. In fact the modal() function returns this element which makes the code trivial.
Best Answer
Yeah, this was hard to figure out. I could not find any example of this in Magento docs.
I wanted to be able to bind button/click events and data to the popup, so I could call methods in my component based on user input in those popups.
Just putting a click bind on the model dom did not fire as when Magento model copies the content it does not have knockout bindings applied!
First your js needs to define the appropriate things like Magento_UI/js/modal/modal
In our content.html we want to add a popup such as.
There is probably a proper way to do this, it feels like a hack but hey I've spent way too long on this.
I did find some examples/discussion which could be handy but seemed way complex/verbose...
Doing it on separate records. https://jsfiddle.net/BitWiseGuy/4u5egybp/
https://stackoverflow.com/questions/10626780/knockout-js-deferred-databinding-for-modal
Yes I could have used bootstrap but I figured magento already has a way to do it.