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:
We could have added an external function that is used inside a calculation to show its use inside the ViewModel. You are still left with some work to do as you will have to choose the best place to put things like that. Perhaps, in a couple of versions, these tools will just magically do it all for us. Well, even if they did, there will be new needs and we will still have a good opportunity to solve issues through code for those we serve.