Skip to content
New issue

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

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

Already on GitHub? # to your account

getObservable return declared normal observable. #60

Merged
merged 3 commits into from
Oct 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/knockout-es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@
if (allObservablesForObject && propertyName in allObservablesForObject) {
return allObservablesForObject[propertyName]();
}

var observable = obj[propertyName];
if (ko.isObservable(observable)) {
return observable;
}

return null;
}
Expand Down
10 changes: 6 additions & 4 deletions test/utility-functions.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
describe('Utility functions', function () {

describe('ko.getObservable()', function () {

it('returns the observable corresponding to a given property', function () {
var obj = ko.track({ alpha: 1, beta: 2 }),
observable = ko.getObservable(obj, 'alpha');
var vm = { alpha: 1, beta: 2, gamma: ko.observable(3) };
var obj = ko.track(vm),
observable = ko.getObservable(obj, 'alpha'),
normalDeclaredObservable = ko.getObservable(obj, 'gamma');

// Check we can both read and write the corresponding property value
assert.equal(observable(), 1);
Expand All @@ -14,6 +15,7 @@ describe('Utility functions', function () {
observable.subscribe(function(newVal) { receivedValue = newVal; });
obj.alpha = 'New value';
assert.equal(receivedValue, 'New value');
assert.equal(normalDeclaredObservable(), 3);
});

it('returns null if the given object isn\'t an object', function() {
Expand Down Expand Up @@ -61,4 +63,4 @@ describe('Utility functions', function () {
});

});
});
});