为什么从 ko.mapping.fromJS 更新值后,选项文本会转换为函数字符串?
示例:http: //jsfiddle.net/tYnc6/24/
html:
<div>
<select data-bind="options: items, value: selected, optionsText: function(item) { return ('[' + item.Id + '] ' + item.Name) }, optionsCaption: 'Choose...'"></select>
<button data-bind="click: update">Update</button>
</div>
Javascript:
var mapping = {
key: function(data) {
return ko.utils.unwrapObservable(data.Id);
}
};
viewModel = {
items: ko.observableArray([
{Name: 'foo', Id: '1'},
{Name: 'bar', Id: '2'}
]),
selected: ko.observable(),
update: function() {
data = [
{Name: 'foo', Id: '1'},
{Name: 'bar', Id: '2'},
{Name: 'baz', Id: '3'}
];
ko.mapping.fromJS(data, mapping, this.items);
}
}
ko.applyBindings(viewModel);
请注意,按下更新后,选项文本变成了一个函数。