O'Reilly logo

KnockoutJS Web Development by John Farrar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with non-Knockout functions

Now, we will add an external function to show an alternate way to format values in the View. You are going to make one more pass at modifying the computed.htm example we just worked on. First at the top of the script, add a function with the following code:

dollarFormat = function(value){
    return "$ "+value;
}

Now go into the View and change the final total as follows. Test it to make sure you got it right and then change all of them if you desire:

<span data-bind="text:dollarFormat(total())"></span>

This is what the page looks like with the last item formatted as a dollar amount:

Working with non-Knockout functions

We could have added an external function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required