From 2dd6f8a3ce60c1eb3bb4f5ffa3dd88cb3a8ba659 Mon Sep 17 00:00:00 2001 From: Rafael Weinstein Date: Thu, 29 Aug 2013 10:56:20 -0700 Subject: [PATCH] Expose setValue on PathObserver R=arv BUG= Review URL: https://codereview.appspot.com/13393044 --- src/observe.js | 12 +++++++++++- tests/test.js | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) 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);