Knockout comes with a binding to show elements (visible), but not one to hide elements. So here it is:

ko.bindingHandlers['hidden'] =
  update: (element, valueAccessor) ->
    value = ko.utils.unwrapObservable(valueAccessor())
    isCurrentlyVisible = (element.style.display != "none")

    if (value && isCurrentlyVisible)
      element.style.display = "none"
    else if (!value && !isCurrentlyVisible)
      element.style.display = ""
  • Categories
comments powered by Disqus