Skip to content

Commit

Permalink
Merge pull request #32 from canjs/key-value
Browse files Browse the repository at this point in the history
Add a value property getter & setter to the key module/observable
  • Loading branch information
chasenlehara committed Jul 4, 2018
2 parents c2475bf + 73916d0 commit cf91ec2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
13 changes: 13 additions & 0 deletions key/key-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ QUnit.test("basics", function(assert) {
assert.equal(canReflect.getValue(observable), "aloha", "getValue unbound");
});

QUnit.test("value property is a getter and setter", function(assert) {
var outer = {inner: {key: "hello"}};
var observable = keyObservable(outer, "inner.key");

// Check getting the value
assert.equal(observable.value, "hello", "value getter works");

// Check setting the value
observable.value = "aloha";
assert.equal(canReflect.getValue(outer).inner.key, "aloha", "value setter sets other object");
assert.equal(canReflect.getValue(observable), "aloha", "value getter works after set");
});

QUnit.test("get and set Priority", function(assert) {
var outer = {inner: {key: "hello"}};
var observable = keyObservable(outer, "inner.key");
Expand Down
15 changes: 12 additions & 3 deletions key/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@ module.exports = function keyObservable(root, keyPath) {
return value;
});

// Function for setting the value
var valueSetter = function(newVal) {
canKey.set(root, keyPathParts, newVal);
};

// The `value` property getter & setter
Object.defineProperty(observation, "value", {
get: observation.get,
set: valueSetter
});

var symbolsToAssign = {
"can.setValue": function(newVal) {
canKey.set(root, keyPathParts, newVal);
}
"can.setValue": valueSetter
};

//!steal-remove-start
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"can-key-tree": "^1.0.0",
"can-log": "^1.0.0",
"can-namespace": "1.0.0",
"can-observation": "^4.0.0",
"can-observation": "^4.1.0",
"can-observation-recorder": "^1.0.0",
"can-queues": "^1.0.0",
"can-reflect": "^1.10.1",
Expand Down

0 comments on commit cf91ec2

Please sign in to comment.