Basically I want visible: to fire up only on the div i hover over, but it applies to all of them,
no matter which one I hover over.
I guess that makes sense, since I am binding to visible: inside foreach: loop, so it is applied to all of them. Is there a KnockoutJS work-around, or should I just use $jQuery.hover() to make it work instead?
Html:
<div>
<div data-bind="foreach: hostGroups">
<div data-bind="css: { style: true }, event: { mouseover: $root.showDeleteLink, mouseout: $root.hideDeleteLink }">
<span data-bind="text: hostName"></span>
<span class="delete-link" data-bind="visible: $root.deleteLinkVisible">this is a delete link</span>
</div>
</div>
</div>
JavaScript:
var data = [
{ "hostName": "server1" },
{ "hostName": "server2" },
{ "hostName": "server3" }
];
var viewModel = {
hostGroups: ko.observableArray(data),
deleteLinkVisible: ko.observable(false),
showDeleteLink: function() {
viewModel.deleteLinkVisible(true);
},
hideDeleteLink: function() {
viewModel.deleteLinkVisible(false);
}
};
ko.applyBindings(viewModel);
You should implement hiding and showing the link on each individual item instead of on the root view model. For example:
JavaScript:
Html:
Example: http://jsfiddle.net/KXtTU/4/