Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visibility style binding using knockout.js fails

data-bind="style : { display : repeat() === 'Custom' ? 'block' : 'none' }"

This style binding succeeds using knockout where as the following fails

data-bind="style : { visibility : repeat() === 'Custom' ? 'visible' : 'hidden' }"

Why?

I can use visible binding but in my case I don't want to lose that div space even when it is hidden.

How can I achieve this?

I don't want to make this happen using jquery as I have already succeeded using it.

like image 998
Govan Avatar asked Feb 13 '14 14:02

Govan


People also ask

What are the types of binding supported by knockout JS?

Binding Values The binding value can be a single value, literal, a variable or can be a JavaScript expression.

What is binding in knockout JS?

A binding consists of two items, the binding name and value, separated by a colon. Here is an example of a single, simple binding: Today's message is: <span data-bind= "text: myMessage" ></span> An element can include multiple bindings (related or unrelated), with each binding separated by a comma.

How do you write if condition in knockout JS?

Note: Using “if” and “ifnot” without a container element-- ko --> and <! -- /ko --> comments act as start/end markers, defining a “virtual element” that contains the markup inside. Knockout understands this virtual element syntax and binds as if you had a real container element.

How do I activate knockout JS?

Activating Knockoutko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.


3 Answers

Another option to solve this issue is to create your own binding. That sounds hard, but it's really easy and KO was designed with custom binding in mind. I wish the base package had more of them, but they are trivial to create. The advantage to this solution is that your binding is simple and more legible. Here is an example, called hidden:

ko.bindingHandlers.hidden = (function() {
    function setVisibility(element, valueAccessor) {
        var hidden = ko.unwrap(valueAccessor());
        $(element).css('visibility', hidden ? 'hidden' : 'visible');
    }
    return { init: setVisibility, update: setVisibility };
})();

And used in your HTML as:

data-bind="hidden: !repeat()"
like image 73
raider33 Avatar answered Nov 02 '22 06:11

raider33


I just did something very similar and this worked for me:

data-bind="style : { visibility : repeat() === 'Custom' ? '' : 'hidden' }"

The difference is setting visibility to '' rather than visible.

like image 43
James Hulse Avatar answered Nov 02 '22 06:11

James Hulse


Would probably be better to use the css binding http://knockoutjs.com/documentation/css-binding.html. You could also tidy the whole thing with a computed observable.

self.hidden = ko.computed(function() {
    return self.repeat() !== 'Custom';
})

Your binding is now this simple data-bind="css: { hide: hidden }"

In your CSS provide the following class:

.hide {
    visibility: 'hidden';
}
like image 31
Michael Papworth Avatar answered Nov 02 '22 07:11

Michael Papworth