JSFiddle - React, Tailwind, and code Playground
by Francesco Frustaci
HTML
<button data-bind="clickConfirm:{ clickMe }">Click me</button>
JavaScript
ko.bindingHandlers.clickConfirm = {
'init': function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var newValueAccessor = function () {
var message = ko.unwrap(value.message);
var result = {};
result.click = function () {
var result = confirm(message);
// If they press OK,
console.log('pressed - ', result);
if (result === true) {
console.log('Calling this - ', valueAccessor());
valueAccessor()();
}
}
return result;
};
return ko.bindingHandlers['event']['init'].call(this, element, newValueAccessor, allBindings, viewModel, bindingContext);
}
}
function viewModel() {
var self = this;
self.clickMe = function () {
alert('confirmed');
}
}
ko.applyBindings(new viewModel());