2

我尝试这样做:

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 计算正确。但是当我改变 weight 或 anotherWeight 字段时 - computedWeight 不会改变。
谢谢。

4

1 回答 1

0

Try ko.mapping.fromJSON() instead of ko.mapping.fromJS()

于 2012-10-21T19:14:09.147 回答