As Sensei has written earlier, KnockOutJS is a great framework for creating rich client side solutions for you web applications. Simply said it cuts your development down considerably by performing CSS binding for you, while also bringing better structure to your Javascript through the use of the MVVM pattern. But even the greatest of all wizardry, magery, grammary, magik has its stumbling blocks. Each tool you use constrains you in some way. This week Sensei uncovered another puzzle that has left him wondering still if he found the best solution. Maybe you will have some insight you can share with the “options” binding from Knockout.

Here is the scenario: You have a <select> ( or a drop down list as us old school Windows devs are found of saying) that you wish to populate with values from an array. There are two ways that this select list will be used. The first is when you create a record, and naturally you would like the list to display “New …”. The second goal is set the value of the <select> to match the value of a current record. Here is the JS-Fiddle with the first attempt. Selecting from the list sets the value, and you’ll the update at the bottom. Clicking “Simulate Editing Amys Record” will set the value of the list to “Amy” as though you were performing an edit operation. Here is the view model code:

Now bear with Sensei as he describes the behavior that was so confounding. We are initializing the <select> by setting data.selectedValue(“”). The first time the page is displayed we get the behavior that we want. Click the “Set List for New Record List”. Nothing happens. If you trace with Firebug you’ll see that the value is indeed being set, but once it leaves the method it reverts to the current value in the <select>.

Speak, friend, and enter …

Oookkay. Scratch your head. Walk away. Come back, fiddle so more. Rinse, then repeat for about 5 hours. This shouldn’t be. In his frenzy Sensei did not consult StackOverflow. At last an idea came to mind. Why not add “New …” as the first entry in <select>, give a value of -1 and an ID of -1. This way at least it is identifiable. Seems silly but when you have things to accomplish sometimes you just have to eat the sausage instead of thinking about how its made. Check out the new JS Fiddle before Sensei explains.

Three simple changes have occured. First we cheate by adding a new object to the array that supports our <select>. We gave it the “New …” as the first element.

We yanked out the “optionsCaption: ‘New …” entry in the HTML mark for the view. Finally the altered the method self.prepForNew to set the value of selectedValue to “New …” with the statement selectedValue(“New …”); This forces KnockOut to sync to what we want. Remember that we are working with methods when setting values with Knockout, hence the use of (“New …”);

Sensei is happy to have things working. Being perplexed over finding out the cause instead of simply creating something simple did fret away the hours. Like with any new tool, there are nuances that won’t become apparent until you are hit over the head with their pitfalls.