-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyEmpty.js
39 lines (37 loc) · 1.35 KB
/
anyEmpty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*jshint -W069 */ /* leaving in [] notation to match knockout */
function hasItems(data) {
var value = ko.unwrap(data);
return value.length && value.length > 0;
}
/**
* renders when the collection has items. Can be used
* on with virtual elements.
* @param anyArray any array observable or not
* @example <!-- ko any: collection -->content<!-- /ko -->
*/
ko.bindingHandlers.any = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
return ko.bindingHandlers['if']['init'](element, function () {
return ko.computed(function () {
return hasItems(valueAccessor());
});
}, allBindings, viewModel, bindingContext);
}
};
ko.virtualElements.allowedBindings.any = true;
/**
* renders when the collection is empty. Can be used
* on with virtual elements.
* @param anyArray any array observable or not
* @example <!-- ko empty: collection -->content<!-- /ko -->
*/
ko.bindingHandlers.empty = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
return ko.bindingHandlers['if']['init'](element, function () {
return ko.computed(function () {
return !hasItems(valueAccessor());
});
}, allBindings, viewModel, bindingContext);
}
};
ko.virtualElements.allowedBindings.empty = true;