gpt4 book ai didi

twitter-bootstrap - knockout Twitter Bootstrap 弹出框绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 11:09:26 24 4
gpt4 key购买 nike

我正在尝试为引用模板的 twitter boostrap 弹出框创建自定义绑定(bind),但是一旦创建弹出框内的内容的绑定(bind)部分,我遇到了问题。

我以前看过这个问题,但我觉得它们大多都很困惑,而且我非常接近一个可重用的解决方案,它可以按照我的意愿使用模板。

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"
}
};

===编辑

根据下面的答案更新了代码,允许您在没有额外的 withProperties 绑定(bind)的情况下执行此操作
// 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"
}
};

最佳答案

您需要使用我的老 friend ,custom bindings .

ko.bindingHandlers.withProperties = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// Make a modified binding context, with a extra properties, and apply it to descendant elements
var newProperties = valueAccessor(),
innerBindingContext = bindingContext.extend(newProperties);
ko.applyBindingsToDescendants(innerBindingContext, element);

// Also tell KO *not* to bind the descendants itself, otherwise they will be bound twice
return { controlsDescendantBindings: true };
}
};

然后,您需要在生成的 html 中添加一个 data-bind 属性:
    var tmplDom = $('<div/>', {
"class": "ko-popover",
"id": domId,
"data-bind": "withProperties: { label: '" + viewModel.label() + "', required: '" + viewModel.required() + "' }"

我整理了一个 jsFiddle显示这一点。有几个陷阱,我必须为每个弹出框复制弹出框选项,否则它们都会以最后一组值结束。
    var popoverOptions = $.extend({}, ko.bindingHandlers.popover.options);
popoverOptions.content = options.content;

而且我还必须仅在弹出窗口可见时才对弹出窗口应用绑定(bind),否则它似乎试图绑定(bind)到整个页面。
$(element).bind('click', function () {
$(this).popover(popoverOptions).popover('toggle');
// If you apply this when the popup isn't visible, I think that it tries to bind to thewhole pageand throws an error
if($('#' + domId).is(':visible'))
{
ko.applyBindings(viewModel, $('#' + domId)[0]);
}
});

这似乎也是双向的,因为您可以更改弹出窗口中的值并更新非弹出元素,但我不会撒谎,我没想到会发生这种情况!

关于twitter-bootstrap - knockout Twitter Bootstrap 弹出框绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14822018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com