Skip to content

Commit

Permalink
Throttled observables get updated correctly. Fixes #99.
Browse files Browse the repository at this point in the history
  • Loading branch information
sagacity committed Aug 3, 2012
1 parent d93196b commit a9599a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
16 changes: 12 additions & 4 deletions knockout.mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,22 +371,28 @@
default:
if (ko.isWriteableObservable(mappedRootObject)) {
if (hasUpdateCallback()) {
mappedRootObject(updateCallback(mappedRootObject));
var valueToWrite = updateCallback(mappedRootObject);
mappedRootObject(valueToWrite);
return valueToWrite;
} else {
mappedRootObject(ko.utils.unwrapObservable(rootObject));
var valueToWrite = ko.utils.unwrapObservable(rootObject);
mappedRootObject(valueToWrite);
return valueToWrite;
}
} else {
if (hasCreateCallback()) {
mappedRootObject = createCallback();
return mappedRootObject;
} else {
mappedRootObject = ko.observable(ko.utils.unwrapObservable(rootObject));
return mappedRootObject;
}

if (hasUpdateCallback()) {
mappedRootObject(updateCallback(mappedRootObject));
return mappedRootObject;
}
}
break;
}

} else {
Expand Down Expand Up @@ -431,7 +437,9 @@
// In case we are adding an already mapped property, fill it with the previously mapped property value to prevent recursion.
// If this is a property that was generated by fromJS, we should use the options specified there
var prevMappedProperty = visitedObjects.get(rootObject[indexer]);
var value = prevMappedProperty || updateViewModel(mappedRootObject[indexer], rootObject[indexer], options, indexer, mappedRootObject, fullPropertyName);
var retval = updateViewModel(mappedRootObject[indexer], rootObject[indexer], options, indexer, mappedRootObject, fullPropertyName);
debugger;
var value = prevMappedProperty || retval;

if (ko.isWriteableObservable(mappedRootObject[indexer])) {
mappedRootObject[indexer](ko.utils.unwrapObservable(value));
Expand Down
17 changes: 17 additions & 0 deletions spec/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,21 @@ test('Issue #94', function() {
equal(viewModel.prop(), "edit");
equal(viewModel.obj.prop(), "original");
equal(viewModel.obj.obj.prop(), "original");
});

asyncTest('Issue #99', function() {
var a = {
x : ko.observable().extend({ throttle: 1 })
};

var receivedValue;
a.x.subscribe(function(value) {
receivedValue = value;
});

ko.mapping.fromJS({ x: 3 }, {}, a);
window.setTimeout(function() {
equal(receivedValue, 3);
start();
}, 2);
});

0 comments on commit a9599a8

Please sign in to comment.