Skip to content

Commit

Permalink
Add class as an alias of css. This allows for combined static and dyn…
Browse files Browse the repository at this point in the history
…amic class bindings.
  • Loading branch information
mbest committed Nov 20, 2015
1 parent 281e195 commit 75abab1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions spec/defaultBindings/cssBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,31 @@ describe('Binding: CSS classes', function() {
observable1("my-Rule");
expect(testNode.childNodes[0].className).toEqual("unrelatedClass1 my-Rule");
});

it('Should be aliased as class as well as css', function() {
expect(ko.bindingHandlers['class']).toEqual(ko.bindingHandlers.css);
});

it('Should be able to combine "class" and "css" bindings with dynamic and static classes', function () {
// This test doesn't cover cases where the static and dynamic bindings try to set or unset the same class name
// because the behavior for that scenario isn't defined.

var booleanProp = new ko.observable(false);
var stringProp = new ko.observable("");
testNode.innerHTML = "<div class='unrelatedClass' data-bind='css: { staticClass: booleanProp }, class: stringProp'></div>";

ko.applyBindings({ booleanProp: booleanProp, stringProp: stringProp }, testNode);
expect(testNode.childNodes[0].className).toEqual("unrelatedClass");

booleanProp(true);
expect(testNode.childNodes[0].className).toEqual("unrelatedClass staticClass");

stringProp("dynamicClass");
expect(testNode.childNodes[0].className).toEqual("unrelatedClass staticClass dynamicClass");

booleanProp(false);
expect(testNode.childNodes[0].className).toEqual("unrelatedClass dynamicClass");
stringProp(null);
expect(testNode.childNodes[0].className).toEqual("unrelatedClass");
});
});
2 changes: 1 addition & 1 deletion src/binding/defaultBindings/css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var classesWrittenByBindingKey = '__ko__cssValue';
ko.bindingHandlers['css'] = {
ko.bindingHandlers['class'] = ko.bindingHandlers['css'] = {
'update': function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (value !== null && typeof value == "object") {
Expand Down

0 comments on commit 75abab1

Please sign in to comment.