hroerdi...@gmail.com
unread,Sep 18, 2012, 10:04:23 AM9/18/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to knock...@googlegroups.com
First of all, I know there have been one or two questions very similar to mine, but the solution posted there (using the if-binding) isn't viable for me. Hence the new discussion. Now for the actual problem:
A commonly used pattern in the forms in my application is one where a user selects a category for the entity they are creating. They can either pick a category from a select control, or define a new category by filling an input field as they please. In our old (non-knockout) setup, this worked fine. With knockout, there's a problem I can't seem to overcome: both controls are bound to the same observable, since that observable is committed to the backend when the form is submitted. So it sort of looks like this:
viewModel: {
categories: ko.observableArray(['one', 'two', 'you know the rest']),
selectedCategory: ko.observable('one')
}
HTML:
<input type="text" data-bind="value: selectedCategory" />
<select data-bind="options: categories, value: selectedCategory" />
The select control works fine: I get a default selection, and I can change it at will. This is propagated to the input field and committed properly. However, when valueUpdate occurs for the input field (when I try to define a new category), the select control tries to update it's selection, can't find the correct item, and it will revert back to selecting the first item in the array. This prevents me from being able to define a new category.
I know about the optionsCaption binding, and I guess I could use this to set the first item to a hardcoded value that I can just filter for by making a computed observable, but using a caption like that is not desired in these cases (long story, product managers...)
Currently I am at a loss on how to fix this. Does anyone see the light where I can't? Thanks in advance!