Last active
August 29, 2015 14:00
-
-
Save osbornm/487e5346d397ffa66260 to your computer and use it in GitHub Desktop.
Any & Empty Bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasItems(data) { | |
var value = ko.unwrap(data), | |
result = false; | |
if (value.length && value.length > 0) | |
result = true; | |
return result; | |
} | |
ko.bindingHandlers.any = { | |
init: function (element, valueAccessor, allBindings) { | |
var result = hasItems(valueAccessor()); | |
return ko.bindingHandlers['if']['init'](element, function () { return result; }, allBindings); | |
}, | |
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) { | |
var result = hasItems(valueAccessor()); | |
ko.bindingHandlers['if']['update'](element, function () { return result; }, allBindings, viewModel, bindingContext); | |
} | |
}; | |
ko.virtualElements.allowedBindings.any = true; | |
ko.bindingHandlers.empty = { | |
init: function (element, valueAccessor, allBindings) { | |
var result = !hasItems(valueAccessor()); | |
return ko.bindingHandlers['if']['init'](element, function () { return result; }, allBindings); | |
}, | |
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) { | |
var result = !hasItems(valueAccessor()); | |
ko.bindingHandlers['if']['update'](element, function () { return result; }, allBindings, viewModel, bindingContext); | |
} | |
}; | |
ko.virtualElements.allowedBindings.empty = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ko any: someArrayOrObservableArray --> | |
<h1>Has Some</h1> | |
<!-- /ko--> | |
<!-- ko empty: someArrayOrObservableArray --> | |
<h1>Empty</h1> | |
<!-- /ko--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The implementation is a little crud and could be cleaned up a lot but is this something the @knockoutjs team wants?