Skip to content

Instantly share code, notes, and snippets.

@anishpateluk
Created November 10, 2015 12:32
Show Gist options
  • Save anishpateluk/cac834b36603e702aee1 to your computer and use it in GitHub Desktop.
Save anishpateluk/cac834b36603e702aee1 to your computer and use it in GitHub Desktop.
ko.observableArray.fn.trackHasItems = function () {
//create a sub-observable
this.hasItems = ko.observable();
//update it when the observableArray is updated
this.subscribe(function (newValue) {
this.hasItems(newValue && newValue.length);
}, this);
//trigger change to initialize the value
this.valueHasMutated();
//support chaining by returning the array
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment