[英]Knockout JS binding "options" is not changing the whole object
I have following code:我有以下代码:
models.js模型.js
class MyModel {
constructor(value, title) {
this.value = ko.observable(value);
this.title = ko.observable(title);
}
}
class MyWrapper {
constructor(selected_model) {
this.selected_model = ko.observable(selected_model);
}
}
view_model.js view_model.js
class MyVM {
constructor() {
let items = [
new MyModel(1, 'One'),
new MyModel(2, 'Two'),
new MyModel(3, 'Three')
];
this.object_to_work_with = ko.observable(new MyWrapper(items[1]));
this.my_list = ko.observableArray(items);
}
}
html template html模板
<div data-bind="with: object_to_work_with">
<select id="my_list" data-bind="options: $root.my_list,
optionsValue: 'value',
optionsText: 'title'
value: selected_model().value"></select>
</div>
And now the issue.现在的问题。 At first I thought the value should be
selected_model
directly:一开始我以为值应该直接
selected_model
:
<div data-bind="with: object_to_work_with">
<select id="my_list" data-bind="options: $root.my_list,
optionsValue: 'value',
optionsText: 'title'
value: selected_model"></select>
</div>
but this do not work, it causes an error like "undefined has no property 'value'".但这不起作用,它会导致像“未定义的没有属性'值'”这样的错误。 So I've tried
value: selected_model()
, now it worked and correct value was selected, but as literal -> if I changed it, object_to_work_with.selected_model()
is not changed.所以我尝试了
value: selected_model()
,现在它工作并且选择了正确的值,但是作为文字 - >如果我改变它, object_to_work_with.selected_model()
不会改变。 And the last try was value: selected_model().value
and it seemed to work, but now when I select another value, it does not change object_to_work_with.selected_model()
, it changes only the value of selected model, I mean this object_to_work_with.selected_model().value() => <the value I've selected>
, but object_to_work_with.selected_model().title() => 'Two'
is never changed.最后一次尝试是
value: selected_model().value
,它似乎工作,但现在当我 select 另一个值时,它不会改变object_to_work_with.selected_model()
,它只改变选定 model 的值,我的意思是这个object_to_work_with.selected_model().value() => <the value I've selected>
,但object_to_work_with.selected_model().title() => 'Two'
永远不会改变。
I am searching a solution quite long time and I have no idea how to solve it.我正在寻找一个解决方案很长时间,但我不知道如何解决它。 According to the manual it looks like it should be
value: selected_model
, but it causes the exception and I have no idea what am I doing wrong.根据手册,它看起来应该是
value: selected_model
,但它会导致异常,我不知道我做错了什么。
Any idea please?请问有什么想法吗?
Edit: And moreover the last option value: selected_model().value
is changing the value of the item in my_list
, because it is of course only reference.编辑:此外,最后一个选项
value: selected_model().value
正在更改my_list
中项目的值,因为它当然只是参考。
Edit 2: Here I've created a working demo - https://jsfiddle.net/L7tsy4ae/ .编辑 2:在这里我创建了一个工作演示 - https://jsfiddle.net/L7tsy4ae/ 。 Just try to use selectbox.
只需尝试使用选择框。
If you decide to go with the optionsValue: 'value'
binding, you also have to initialize the selected_model
with items[1]
..value
如果您决定使用
optionsValue: 'value'
绑定 go,您还必须使用items[1] .value
初始化selected_model
。 Instead, I'd opt for not using the optionsValue
binding at all so your selection can point to the actual viewmodel.相反,我会选择根本不使用
optionsValue
绑定,这样您的选择就可以指向实际的视图模型。
Here's a working example (with some other other changes not strictly needed: removed the observable wrapper of object_to_work_with
and replaced the change
event binding with a subscribe
inside the viewmodel.)这是一个工作示例(其他一些并非严格需要的更改:删除了
object_to_work_with
的可观察包装,并将change
事件绑定替换为视图模型中的subscribe
。)
class MyModel { constructor(value, title) { this.value = ko.observable(value); this.title = ko.observable(title); } } class MyWrapper { constructor(selected_model) { this.selected_model = ko.observable(selected_model); } } class MyVM { constructor() { let items = [ new MyModel(1, 'One'), new MyModel(2, 'Two'), new MyModel(3, 'Three') ]; this.object_to_work_with = new MyWrapper(items[1]); this.my_list = ko.observableArray(items); this.object_to_work_with.selected_model.subscribe(this.print_data, this); } print_data(selected_model) { const div = document.createElement('div'); const text = document.createTextNode( `${selected_model.title()}: ${selected_model.value()}` ); div.appendChild(text); document.getElementById('data').appendChild(div); } } ko.applyBindings(new MyVM());
#content { padding: 30px; margin: 30px; background-color: #e9e9e9; border: 1px solid #999999; border-radius: 6px; } #data { border-radius: 6px; background-color: #ffffff; padding: 20px; margin-top: 20px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <div id="content" data-bind="with: object_to_work_with"> <select id="my_list" data-bind="options: $root.my_list, optionsText: 'title', value: selected_model"></select> <div id="data"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.