Ich versuche eine benutzerdefinierte Bindung für Twitter Booster Popovers zu erstellen, die auf eine Vorlage verweist, aber ich habe Probleme mit dem verbindlichen Teil des Inhalts innerhalb des Popover, sobald es erstellt wurde.Knockout Twitter Bootstrap Popover Bindung
Ich habe diese Frage schon einmal gesehen, aber ich habe das Gefühl, dass sie meistens ziemlich unordentlich sind und ich bin ziemlich nah an einer wiederverwendbaren Lösung, die Vorlagen benutzt, wie ich will.
http://jsfiddle.net/billpull/Edptd/
// Bind Twitter Popover
ko.bindingHandlers.popover = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var tmplId = ko.utils.unwrapObservable(valueAccessor());
var tmplHtml = $('#' + tmplId).html();
var uuid = guid();
var domId = "ko-bs-popover-" + uuid;
var tmplDom = $('<div/>', {
"class" : "ko-popover",
"id" : domId
}).html(tmplHtml);
options = {
content: tmplDom[0].outerHTML
};
var popoverOptions = ko.utils.extend(ko.bindingHandlers.popover.options, options);
console.log($(element));
console.log(element);
$(element).bind('click', function() {
$(this).popover(popoverOptions).popover('toggle');
ko.applyBindings(bindingContext, document.getElementById(domId));
});
},
options: {
placement: "right",
title: "",
html: true,
content: "",
trigger: "manual"
}
};
=== EDIT
aktualisiert Code basierend auf Antwort unten, dass man es ohne die zusätzlichen withProperties zu tun erlaubt Bindung
// Bind Twitter Popover
ko.bindingHandlers.popover = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// read popover options
var popoverBindingValues = ko.utils.unwrapObservable(valueAccessor());
// set popover template id
var tmplId = popoverBindingValues.template;
// set popover trigger
var trigger = popoverBindingValues.trigger;
// get template html
var tmplHtml = $('#' + tmplId).html();
// create unique identifier to bind to
var uuid = guid();
var domId = "ko-bs-popover-" + uuid;
// create correct binding context
var childBindingContext = bindingContext.createChildContext(viewModel);
// create DOM object to use for popover content
var tmplDom = $('<div/>', {
"class" : "ko-popover",
"id" : domId
}).html(tmplHtml);
// set content options
options = {
content: tmplDom[0].outerHTML
};
// Need to copy this, otherwise all the popups end up with the value of the last item
var popoverOptions = $.extend({}, ko.bindingHandlers.popover.options);
popoverOptions.content = options.content;
// bind popover to element click
$(element).bind(trigger, function() {
$(this).popover(popoverOptions).popover('toggle');
// if the popover is visible bind the view model to our dom ID
if($('#' + domId).is(':visible')){
ko.applyBindingsToDescendants(childBindingContext, $('#' + domId)[0]);
}
});
return { controlsDescendantBindings: true };
},
options: {
placement: "right",
title: "",
html: true,
content: "",
trigger: "manual"
}
};
Was hier eigentlich das Problem? Sie haben nicht beschrieben, was das Problem ist. –
"Ich habe Probleme mit dem verbindlichen Teil des Inhalts innerhalb des Popover, sobald er erstellt wurde." – BillPull
Ich bin sicher, dass es möglich sein muss, obwohl nicht einfach, aber ich habe keine Zeit, um den Moment zu sehen. Ich werde heute Abend nachsehen. –