diff --git a/src/observe.js b/src/observe.js index 82ece8d..1dbc9d6 100644 --- a/src/observe.js +++ b/src/observe.js @@ -583,7 +583,8 @@ } }; - function PathObserver(object, pathString, callback, target, token, valueFn) { + function PathObserver(object, pathString, callback, target, token, valueFn, + setValueFn) { var path = getPath(pathString); if (!path) { // Invalid path. @@ -608,6 +609,7 @@ Observer.call(this, object, callback, target, token); this.valueFn = valueFn; + this.setValueFn = setValueFn; this.path = path; this.connect(); @@ -663,6 +665,14 @@ } this.oldValue = this.value; + }, + + setValue: function(newValue) { + if (!this.path) + return; + if (typeof this.setValueFn === 'function') + newValue = this.setValueFn(newValue); + this.path.setValueFrom(this.object, newValue); } }); diff --git a/tests/test.js b/tests/test.js index a78a772..e27102d 100644 --- a/tests/test.js +++ b/tests/test.js @@ -209,6 +209,24 @@ suite('PathObserver Tests', function() { observer.close(); }); + test('Path setValue', function() { + var arr = {}; + + arr.foo = 'bar'; + observer = new PathObserver(arr, 'foo', callback); + arr.foo = 'baz'; + + observer.setValue('bat'); + assert.strictEqual(arr.foo, 'bat'); + assertPathChanges('bat', 'bar'); + + observer.setValue('bot'); + observer.reset(); + assertNoChanges(); + + observer.close(); + }); + test('Degenerate Values', function() { observer = new PathObserver(null, '', callback); assert.equal(null, observer.value);