由于某种原因,我无法正确创建observableyarray,以便更新foreach项目observable。此外,我无法获得与上述相关的点击绑定的更新模型(observablearay)。
<div>
<!-- THIS NEVER UPDATES IF YOU CHANGE TEXTAREA-->
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
<div data-bind="foreach: Items">
<div data-bind="text: TestProperty"></div>
<textarea data-bind="text: DataInput"></textarea>
<!-- THIS NEVER UPDATES IF YOU CHANGE TEXTAREA-->
<span data-bind="text: DataInput"></span>
</div>
<button data-bind="click: test">Save</button>
</div>
JS
var RiskItemModel = function (item) {
var self = this;
self.DataInput = ko.observable(item.DataInput);
self.TestProperty = ko.observable("test");
};
var PortfolioRiskModel = function (data) {
var self = this;
self.Items = ko.observableArray();
// fill with initial data
if (data) {
$.each(data.Items, function (index, item) {
self.Items.push(ko.observable(new RiskItemModel(item)));
//self.Items.push(new RiskItemModel(item));
});
};
self.test = function () {
// I AM ALWATYS GETTING OLD VALUES HERE!
alert(ko.unwrap(ko.unwrap(self.Items)[0]).DataInput());
};
};
var data = {
"$id": "1",
"Items": [{
"DataInput": "first"
}
/*, {
"DataInput": "second"
}*/
]
};
var ChartSelectedRiskType = new PortfolioRiskModel(data);
ko.applyBindings(ChartSelectedRiskType);
你可以在这里玩jsfiddle:http://jsfiddle.net/L3tJL/
答案 0 :(得分:1)
您已将数据绑定到textarea中的错误属性。应该是这样的:
<textarea data-bind="value: DataInput"></textarea>