Utility functions
There are a number of functions in ko.utils
. Let's start by looking at the special array methods in standard Knockout.
ko.utils.arrayFilter()
The ko.utils.arrayFilter
function allows us to filter items in an array. We are going to run these as straight code examples. We will create a sample JSON file and load it via AJAX to keep the focus on learning the methods and not waste time creating an example code set. We will create a page called utility.html
for these pieces of code and run the filtering code from there. Our markup for this example is here:
<h3>arrayFilter() : staff under 35</h3> <ul data-bind="foreach: youngStaff"> <li><span data-bind="text: age() + ' ' + firstName()"></span></li> </ul>
Our script
code is as follows. We will be adding more for each example as we go, but here are the basics for the utility examples:
<script> var vm = {}; jQuery.getJSON('utility.json').done(function(data){ vm = ko.mapping.fromJS...