我尝试这样做:
myData = {weight: 100, anotherWeight: 120.55}; // when my data is plain json - all work
$.when($.getJSON(url, {param}, function(data) {myData = data} ).
//when i got it from ajax - dosn't work
then(function(){
var taskModel = function(data)
{
var self = this;
ko.mapping.fromJS(data, {}, self);
this.computedWeight = ko.computed(function () { return self.weight() +
' ---- ' +
self.anotherWeight() + ' kg'; });
}
ko.applyBindings(new taskModel(myData));
});
<input type="text" data-bind="value: weight"/><br/>
<input type="text" data-bind="value: anotherWeight"/><br/>
<span data-bind="text: computedWeight"></span>
第一次 - 加载后 - computedWeight计算正确。但是当我改变体重或其他重量字段时 - computedWeight不会改变 感谢。
答案 0 :(得分:0)
尝试使用ko.mapping.fromJSON()
代替ko.mapping.fromJS()