Skip to content

Instantly share code, notes, and snippets.

@tinwatchman
Created April 27, 2017 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tinwatchman/4621d3ea0f2783ac331beffe89c49637 to your computer and use it in GitHub Desktop.
Save tinwatchman/4621d3ea0f2783ac331beffe89c49637 to your computer and use it in GitHub Desktop.
Basic Knockout.js 'hidden' binding. Uses jQuery.
/**
* Knockout Extension: Hidden
*
* The inverse of the `visible` binding: hides the element when the observable
* is true, shows it when the observable is false.
*
* @requires Knockout.js
* @requires jQuery
* @author Jon Stout (www.jonstout.net)
*/
'use strict';
(function(ko, $) {
if (typeof ko.bindingHandlers.hidden === 'undefined') {
ko.bindingHandlers.hidden = {
update: function(element, valueAccessor) {
var isHidden = ko.unwrap(valueAccessor());
if (isHidden) {
$(element).hide();
} else {
$(element).show();
}
}
};
}
})(ko, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment