I would like to add a list of options to a page from a knockout observableArray. When there are more than a configurable number of items to be displayed, a "More options..." button (or link) should be displayed. Pressing this button should display all items and change the button text to "Less options". To make it more interesting: the button should only be displayed when there is more than 1 item that would be hidden.
The code below works (see this fiddle), but isn't there a cleaner and more generic solution (using a custom binding for example)?
<ul data-bind="foreach: options">
<li data-bind="visible: $root.showMore() || $index() < $root.showMoreCount() || $root.options().length <= $root.showMoreCount()+1, text: $data"></li>
</ul>
<a data-bind="visible: options().length-1 > showMoreCount(), text: showMore() ? 'Less options' : 'More options', click: function () { showMore(!showMore()) }"></a>