Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Expose setValue on PathObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Aug 29, 2013
1 parent 5074e78 commit 2dd6f8a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -608,6 +609,7 @@

Observer.call(this, object, callback, target, token);
this.valueFn = valueFn;
this.setValueFn = setValueFn;
this.path = path;

this.connect();
Expand Down Expand Up @@ -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);
}
});

Expand Down
18 changes: 18 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2dd6f8a

Please sign in to comment.