2016-12-01 110 views
0

淘汰赛事件我有一个自定义的酥料饼的结合:自举酥料饼

init = (element: any, valueAccessor:() => any, allBindingsAccessor?: KnockoutAllBindingsAccessor, viewModel?: any, bindingContext?: KnockoutBindingContext) => { 
     var $elem = $(element); 
     var popover = $elem.popover({ 
      placement: 'auto', 
      content: function() { 
       return $('#' + valueAccessor() + " > div"); 
      }, 
      html: true, 
      container: 'body' 
     }) 
} 


<div class="pover"> 
    <div> 
     <span data-bind="click: function(){alert('213')}">test</span> 
    </div> 
</div> 

所以第一次ü开酥料饼 - 它显示文本和点击事件工作正常。 第二次打开popover时,它是空的。 B/c dom在隐藏弹出窗口时会被销毁。 我该如何避免这种情况?我不能克隆HTML,B/C点击事件绑定将被打破...

我的解决方案,请告诉我什么可以出错?

var popover = $elem.popover({ 
      placement: 'auto', 
      content: function() { 
       $("#single-popover").remove(); 
       $('body').append('<div id="single-popover">' + $("#popoverTemplate").html() + '</div>'); 
       ko.cleanNode($('#single-popover')[0]); 
       ko.applyBindings(bindingContext, $('#single-popover')[0]); 
       return $('#single-popover'); 
      }, 
      html: true, 
      container: 'body' 
     }) 
+1

看看[knockstrap(https://faulknercs.github.io/Knockstrap) - 它有助于缩小两者之间的差距图书馆,支持popovers。 –

+1

由于绑定仅被调用一次,因此它内部的html元素一旦被销毁,不会重新构建......我也会建议查看knockstrap – gkb

回答

0

将这样的事情对你的工作http://jsfiddle.net/LkqTU/32668/

ko.bindingHandlers.popover = { 
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { 
    ko.bindingHandlers.value.init(element, valueAccessor, allBindings); 
    var source = allBindings.get('popoverTitle'); 
    var sourceUnwrapped = ko.unwrap(source); 
    $(element).popover({ 
     trigger: 'focus', 
     title: sourceUnwrapped, 
     placement: 'auto', 
     content: function() { 
     return ko.unwrap(valueAccessor()) 
     }, 
     html: true, 
     container: 'body' 
    }); 
    }, 
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) { 
    var value = valueAccessor(); 
    ko.bindingHandlers.value.update(element, valueAccessor); 
    } 
};