Observable array with object with observable properties

127 views
Skip to first unread message

Luc

unread,
Nov 13, 2010, 6:13:48 AM11/13/10
to KnockoutJS
I'm trying to create a select that gets its options from an array,
updates when objects are added/removed to this array and when objects'
properties are updated.

I'm obviously doing something wrong, since the following model/view
pair binds optionText to function content (code) instead of value.

Model

var selectModel = {
availableLanguages: ko.observableArray([
{ Code: ko.observable('SI'), Name:
ko.observable('Slovenian') },
{ Code: ko.observable('EN'), Name: ko.observable('English') },
{ Code: ko.observable('DE'), Name: ko.observable('German') }
])
};


View

<select data-bind="options: availableLanguages, optionsValue: 'Code',
optionsText: 'Name', optionsCaption: 'Choose language...'">

Steven Sanderson

unread,
Nov 15, 2010, 3:27:34 AM11/15/10
to knockoutjs
I'm very surprised by this, but I think you've discovered a use case that hadn't been accounted for. KO should handle this but right now it doesn't.

It's trivial for me to tweak the binding behaviour to work as you expected in this case, so I'll do that in the next day or two.

Sorry about the inconvenience,
Steve

Lance

unread,
Nov 15, 2010, 5:03:45 PM11/15/10
to KnockoutJS
Hello,

I am running into this same issue.

Additionally, I am also seeing some additional behavior that may or
may not be related. If I try to populate an observable array with
observableObjects, it will get an error on binding.

So, if the Model looks like the following, then it will fail to bind
vs binding with the content of the code:

var selectModel = {
availableLanguages: ko.observableArray([
ko.observable({ Code: ko.observable('SI'), Name:
ko.observable('Slovenian')}),
ko.observable({ Code: ko.observable('EN'), Name:
ko.observable('English')}),
ko.observable({ Code: ko.observable('DE'), Name:
ko.observable('German')})]),
};

Lance

unread,
Nov 15, 2010, 5:59:09 PM11/15/10
to KnockoutJS
Oops, I mispoke. I had an error in my binding that caused it to fail
when the array was of observable objects. Now I am seeing the same
behavior.

Steven Sanderson

unread,
Nov 16, 2010, 3:48:26 AM11/16/10
to knockoutjs
Hello again

As promised, I've added support for referencing observable subproperties from the optionsText and optionsValue parameters. This is now in the latest prerelease source code version of KO available at https://github.com/SteveSanderson/knockout/tree/master/build/output/

Cheers
Steve

Luc

unread,
Nov 16, 2010, 4:53:20 AM11/16/10
to KnockoutJS
Yes, I've figured out it's missing a bit of code.

Thanks for the update and thanks for bringing us this library. I've
been in need for something like that for quite some time now. Small,
easy to use and more important, easy to extend (I've created custom
binding handlers for ul trees and jstree the same day I learned about
knockout :-)).

Keep up the good work,

Luc
Reply all
Reply to author
Forward
0 new messages