In general, it is good practice not to inject the logic directly into the knockout binding in HTML. Not the end of the world, but it can quickly lead along a dirty road.
, . , . , / CSS, , , , .
, :
ko.bindingHandlers.hoverTargetId = {};
ko.bindingHandlers.hoverVisible = {
init: function (element, valueAccessor, allBindingsAccessor) {
function showOrHideElement(show) {
var canShow = ko.utils.unwrapObservable(valueAccessor());
$(element).toggle(show && canShow);
}
var hideElement = showOrHideElement.bind(null, false);
var showElement = showOrHideElement.bind(null, true);
var $hoverTarget = $("#" + ko.utils.unwrapObservable(allBindingsAccessor().hoverTargetId));
ko.utils.registerEventHandler($hoverTarget, "mouseover", showElement);
ko.utils.registerEventHandler($hoverTarget, "mouseout", hideElement);
hideElement();
}
};
:
<div><label id='hoverTarget'>Hover to see the details</label></div>
<div data-bind="hoverVisible: hasItems, hoverTargetId: 'hoverTarget'">Here the details</div>
:
- , , , ,
hasItems. checked input type='checkbox' />
source
share