You should check custom bindings in knockout. Here is one simplest custom binding for modal pop which you can use in your case:
ko.bindingHandlers.modal = {
init: function (element, valueAccessors) {
var options = ko.utils.unwrapObservable(valueAccessors() || {});
$(element).modal(options);
},
update: function (element, valueAccessors) {
var options = ko.utils.unwrapObservable(valueAccessors() || {});
$(element).modal(options.show() ? 'show' : 'hide');
}
};
What i understand is that you want a single modal markup code which should be reusable for all modals. This thing is possible with many different ways out of which one is with binding in knockout.Here is the code which demonstrate the use:
JS
function modal(data)
{
var self = this;
self.title = data.title ||
'Default title';
self.message = data.message ||
'Default message';
self.confirmButtonText = data.confirmButtonText ||
'Default text';
self.confirmAction = data.confirmAction ||
function() { alert("Default action"); };
}
function vm()
{
var self = this;
self.addMake = function() { alert("Maked added"); };
self.addCar = function() { alert("Car added"); };
self.MyModels = ko.observableArray([
new modal({
title: "Add new make",
message: "This modal will add new make.",
confirmButtonText: "Add make",
confirmAction: self.addMake
}),
new modal({
title: "Add new car",
message: "This modal will add new car.",
confirmButtonText: "Add car",
confirmAction: self.addCar
})
]);
self.modalVisible = ko.observable(false);
self.SelectedModal = ko.observable(self.MyModels()[0]);
self.openModal = function(data, event)
{
self.SelectedModal(data);
self.modalVisible(true);
};
self.closeModal = function(data, event) { self.modalVisible(false); };
}
Html
<div class="modal fade" data-bind="modal: { backdrop: 'static', keyboard: false, show: modalVisible }">
<div class="modal-dialog" data-bind="with: SelectedModal">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-bind="click: $root.closeModal">×</button>
<span class="modal-title"><span data-bind="text: title"></span></span>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<span data-bind="html: message"></span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-bind="click: $root.closeModal">Cancel</button>
<button type="button" class="btn btn-danger" data-bind="click: confirmAction, text: confirmButtonText"></button>
</div>
</div>
</div>
</div>
Hope is helps!