Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a value property getter & setter to the key module/observable #32

Merged
merged 2 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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