2
我尝试这样做:Knockoutjs + ko.mapping.fromJS +计算字段不计算计算
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不会改变。
谢谢。
是返回服务器的正确的数据?成功回调中返回什么?请看看这[jsfiddle](http://jsfiddle.net/zEfRj/4/) – nemesv
谢谢。在jsfiddle - http://jsfiddle.net/EfRj/4/ - 所有的工作。但在我的情况下 - 计算字段不计算。我将尝试调查ajax响应。 – awzster