Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Select control and input field bound to the same observable
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
hroerdinkhol...@gmail.com  
View profile  
 More options Sep 18 2012, 10:04 am
From: hroerdinkhol...@gmail.com
Date: Tue, 18 Sep 2012 07:04:23 -0700 (PDT)
Local: Tues, Sep 18 2012 10:04 am
Subject: Select control and input field bound to the same observable

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!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rpn  
View profile  
 More options Sep 18 2012, 10:16 am
From: rpn <rnieme...@gmail.com>
Date: Tue, 18 Sep 2012 07:16:47 -0700 (PDT)
Local: Tues, Sep 18 2012 10:16 am
Subject: Re: Select control and input field bound to the same observable

Is it okay for the new item to show up in the dropdown?  How about
something like:  http://jsfiddle.net/rniemeyer/fSZWG/?  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hroerdinkhol...@gmail.com  
View profile  
 More options Sep 18 2012, 10:40 am
From: hroerdinkhol...@gmail.com
Date: Tue, 18 Sep 2012 07:40:48 -0700 (PDT)
Local: Tues, Sep 18 2012 10:40 am
Subject: Re: Select control and input field bound to the same observable

Yes, that will work fine I suppose, thank you! Am I correct that I can not
get away with it NOT being in the list, as long as I want to use the same
value binding in these two places? Just out of curiousity...

Op dinsdag 18 september 2012 16:04:23 UTC+2 schreef hroerdi...@gmail.com
het volgende:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Best  
View profile  
 More options Sep 19 2012, 4:57 pm
From: Michael Best <mb...@dasya.com>
Date: Wed, 19 Sep 2012 13:57:04 -0700 (PDT)
Local: Wed, Sep 19 2012 4:57 pm
Subject: Re: Select control and input field bound to the same observable

Since this is something I've seen come up quite a few times, I've added an
issue in GitHub to add support for this to Knockout:  
https://github.com/SteveSanderson/knockout/issues/647

-- Michael

On Tuesday, September 18, 2012 4:40:48 AM UTC-10, hroerdi...@gmail.com
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Rosenberg  
View profile  
 More options Dec 19 2012, 12:37 pm
From: Jeremy Rosenberg <jeremy.d.rosenb...@gmail.com>
Date: Wed, 19 Dec 2012 09:37:10 -0800 (PST)
Local: Wed, Dec 19 2012 12:37 pm
Subject: Re: Select control and input field bound to the same observable

I second this.

In cases such as these where the range of valid values is constrained,
knockout has to make a decision of what to do when a value falls outside of
that range - either revert the model change, or ignore the change in the UI
(or select nothing like Michael's suggestion).  Right now, it forces that
decision to revert.  An option to control a decision like this is always
preferable.

In my case, I'm trying to emulate an old non-knockout combo-box we have
that uses a text input overlayed on a select.  It is expected that text may
be entered that is not an option in the list, and I would simply like to
ignore the change in the select when this happens since the dropdown text
is not shown anyway.  I have this working, but it requires a large chunk of
pretty grotesque code to workaround this - makes up 90% of the custom
binding, has a dom data footprint, and I feel like it's going to bite me
later.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »