admin管理员组文章数量:1333393
Using knockout.js, is it possible to bind to a property of a child object of a JSON object from the server? Specifically, if I'm given an object from the server that looks like this:
var obj = {
list: [ { key: "a", value: 1 },
{ key: "b", value: 2 },
{ key: "c", value: 3 }
],
selected: {
key: "",
value: null
}
};
I create a viewModel from this javascript object via the "mapping" plugin:
var viewModel = ko.mapping.fromJS(obj);
And I bind list
to a <select>
tag like so:
<select data-bind="options: list, optionsText: 'key',
optionsValue: 'value',
value: selected">
</select>
I've assigned the value to be the selected
property of my viewModel. This means, upon selecting an option, I can successfully query viewModel.selected.key()
and viewModel.selected.value()
in code and get the up-to-date values.
However, I am unable to bind the selected item's key
or value
data to be displayed on a span. For instance, this doesn't display my selected value:
<span data-bind="text: selected.value"></span>
Can I do what I want? Do I need to resort to using a real simple template to establish proper context (ie: selected
)?
I have an example of the situation here. I've even tried specifically mapping the child selected
object to be an observable itself, but with no luck (see mented out mapping call with the additional options).
Using knockout.js, is it possible to bind to a property of a child object of a JSON object from the server? Specifically, if I'm given an object from the server that looks like this:
var obj = {
list: [ { key: "a", value: 1 },
{ key: "b", value: 2 },
{ key: "c", value: 3 }
],
selected: {
key: "",
value: null
}
};
I create a viewModel from this javascript object via the "mapping" plugin:
var viewModel = ko.mapping.fromJS(obj);
And I bind list
to a <select>
tag like so:
<select data-bind="options: list, optionsText: 'key',
optionsValue: 'value',
value: selected">
</select>
I've assigned the value to be the selected
property of my viewModel. This means, upon selecting an option, I can successfully query viewModel.selected.key()
and viewModel.selected.value()
in code and get the up-to-date values.
However, I am unable to bind the selected item's key
or value
data to be displayed on a span. For instance, this doesn't display my selected value:
<span data-bind="text: selected.value"></span>
Can I do what I want? Do I need to resort to using a real simple template to establish proper context (ie: selected
)?
I have an example of the situation here. I've even tried specifically mapping the child selected
object to be an observable itself, but with no luck (see mented out mapping call with the additional options).
1 Answer
Reset to default 8You were on the right track with the mapping options. You would want selected
to be an observable, so your UI updates when you make changes in the dropdown. In your case, it could even be empty.
One thing to note is that when the mapping plugin deals with an object like:
selected: { key="", value=null }
It will make key
and value
observables, but not selected
.
One other issue that you are having is that your first select specifies optionsValue: 'value'
. This will cause it to write the value
property of your object to selected
instead of the object.
If you want to write the object itself, then you would want to pletely remove the optionsValue
binding.
Here is your fiddle with these updates: http://jsfiddle/rniemeyer/Dubu9/2/
本文标签: javascriptBind text to property of child objectStack Overflow
版权声明:本文标题:javascript - Bind text to property of child object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742333441a2455154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论