I've got the following simple setup in knockout
var data = {
'Divisions': [
{ 'divID' : '105' },
{ 'divID' : '103' }
]
};
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
and the following HTML that does not properly bind
<select data-bind="
options: Divisions,
optionsText: divID,
optionsCaption: 'Choose...'">
</select>
onload I get 'ReferenceError: divID is not defined'.
If I use the following binding, it works
<select data-bind="
options: Divisions,
optionsText: function(item) {
return item.divID();
},
optionsCaption: 'Choose...'">
</select>
For reference:
Working binding in jsbin, with the ugly binding
Non working version in jsbin that looks like it should work, with the clean binding
I suspect that this is all being caused by ko.mapping.fromJS making every child in Divisions
an observable, thus making it so that I can't simply access everything as a simple property name, but I just found a similar example that is doing the exact same thing!
I've got the following simple setup in knockout
var data = {
'Divisions': [
{ 'divID' : '105' },
{ 'divID' : '103' }
]
};
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
and the following HTML that does not properly bind
<select data-bind="
options: Divisions,
optionsText: divID,
optionsCaption: 'Choose...'">
</select>
onload I get 'ReferenceError: divID is not defined'.
If I use the following binding, it works
<select data-bind="
options: Divisions,
optionsText: function(item) {
return item.divID();
},
optionsCaption: 'Choose...'">
</select>
For reference:
Working binding in jsbin, with the ugly binding
Non working version in jsbin that looks like it should work, with the clean binding
I suspect that this is all being caused by ko.mapping.fromJS making every child in Divisions
an observable, thus making it so that I can't simply access everything as a simple property name, but I just found a similar example that is doing the exact same thing!
- ARGH, figured it out, stupid stupid stupid mistake – Allen Rice Commented Sep 8, 2011 at 16:49
1 Answer
Reset to default 12You forgot the single quotes around your optionsText
<select data-bind="
options: Divisions,
optionsText: 'divID',
optionsCaption: 'Choose...'">
</select>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744171369a4561550.html
评论列表(0条)