2

I try to do this:

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>

First time - after loading - computedWeight is calculate right. But when I change weight or anotherWeight fields - computedWeight don't change.
Thanks.

awzster
  • 79
  • 7
  • Is the right data is returned for the server? What is returned in the success callback? Please have a look on this [jsfiddle](http://jsfiddle.net/zEfRj/4/) – nemesv Oct 21 '12 at 15:24
  • thanks. In jsfiddle - http://jsfiddle.net/EfRj/4/ - all work. But in my case - computed field is not calculated. I will try to investigate the ajax response. – awzster Oct 21 '12 at 17:01

1 Answers1

0

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

Alexander Taran
  • 6,655
  • 2
  • 39
  • 60
  • Thanks all.The problem was solved. There is my error. I use custom binding for weight & anotherWeight. In this case simple computed(...) does't work. When I use simple bndig <... data-bind="value: weight"> - all work.
    Sorry for question. My eeror.
    – awzster Oct 22 '12 at 06:36