Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
55 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
describe('Binding: Visible/Hidden', function() { | ||
beforeEach(jasmine.prepareTestNode); | ||
|
||
it('Visible means the node is only visible when the value is true', function () { | ||
var observable = new ko.observable(false); | ||
testNode.innerHTML = "<input data-bind='visible:myModelProperty()' />"; | ||
ko.applyBindings({ myModelProperty: observable }, testNode); | ||
|
||
expect(testNode.childNodes[0].style.display).toEqual("none"); | ||
observable(true); | ||
expect(testNode.childNodes[0].style.display).toEqual(""); | ||
}); | ||
|
||
it('Visible should unwrap observables implicitly', function () { | ||
var observable = new ko.observable(false); | ||
testNode.innerHTML = "<input data-bind='visible:myModelProperty' />"; | ||
ko.applyBindings({ myModelProperty: observable }, testNode); | ||
expect(testNode.childNodes[0].style.display).toEqual("none"); | ||
}); | ||
|
||
it('Hidden means the node is only visible when the value is false', function () { | ||
var observable = new ko.observable(false); | ||
testNode.innerHTML = "<input data-bind='hidden:myModelProperty()' />"; | ||
ko.applyBindings({ myModelProperty: observable }, testNode); | ||
|
||
expect(testNode.childNodes[0].style.display).toEqual(""); | ||
observable(true); | ||
expect(testNode.childNodes[0].style.display).toEqual("none"); | ||
}); | ||
|
||
it('Hidden should unwrap observables implicitly', function () { | ||
var observable = new ko.observable(true); | ||
testNode.innerHTML = "<input data-bind='hidden:myModelProperty' />"; | ||
ko.applyBindings({ myModelProperty: observable }, testNode); | ||
expect(testNode.childNodes[0].style.display).toEqual("none"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 16 additions & 10 deletions
26
src/binding/defaultBindings/visible.js → src/binding/defaultBindings/visibleHidden.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
ko.bindingHandlers['visible'] = { | ||
'update': function (element, valueAccessor) { | ||
var value = ko.utils.unwrapObservable(valueAccessor()); | ||
var isCurrentlyVisible = !(element.style.display == "none"); | ||
if (value && !isCurrentlyVisible) | ||
element.style.display = ""; | ||
else if ((!value) && isCurrentlyVisible) | ||
element.style.display = "none"; | ||
} | ||
}; | ||
ko.bindingHandlers['visible'] = { | ||
'update': function (element, valueAccessor) { | ||
var value = ko.utils.unwrapObservable(valueAccessor()); | ||
var isCurrentlyVisible = !(element.style.display == "none"); | ||
if (value && !isCurrentlyVisible) | ||
element.style.display = ""; | ||
else if ((!value) && isCurrentlyVisible) | ||
element.style.display = "none"; | ||
} | ||
}; | ||
|
||
ko.bindingHandlers['hidden'] = { | ||
'update': function (element, valueAccessor) { | ||
ko.bindingHandlers['visible']['update'](element, function() { return !ko.utils.unwrapObservable(valueAccessor()) }); | ||
} | ||
}; |