Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout css binding renders => class=" 0 1 2 3 4 5 6 with KO 2.0

Tags:

knockout.js

I am getting a strange style text with this code:

self.styleClass = ko.computed(function () {
            return self.isFollowing() ? "button" : "secondary button";
        });

      <button data-bind="text: followButtonText,click: toggleIsFollowing, css: styleClass"></button>

it renders:

<button data-bind="text: followButtonText,click: toggleIsFollowing, css: styleClass" class=" 0 1 2 3 4 5 6 7 8 9 10 11 12">Unfollow</button>

model.styleClass() renders fine in the console

like image 324
FutuToad Avatar asked Mar 02 '13 17:03

FutuToad


People also ask

How do I use knockout js in HTML?

It is very easy to use KnockoutJS. Simply refer the JavaScript file using <script> tag in HTML pages. A page as in the following image will be displayed. Click on download link and you will get the latest knockout.

What is data binding in knockout JS?

Knockout's declarative binding system provides a concise and powerful way to link data to the UI. It's generally easy and obvious to bind to simple data properties or to use a single binding.

What does binding style mean?

Style Binding allows you to apply inline styling to HTML DOM element by manipulating the element's style attribute instead of applying CSS classes.


1 Answers

The semantic of the css binding has been changed from KO 2.1.0 to KO 2.2.0

From Knockout 2.2.0 released:

We’ve also made some features work more like you might always have thought they should work. For example, the css binding can now attach programmatically-generated CSS class names to elements (previously, it was limited to toggling predefined CSS class names).

So upgrade to KO 2.2.1 and it will work fine.

If you cannot upgrade you can use the attr binding as a workaround:

<button data-bind="text: followButtonText, 
                   click: toggleIsFollowing,
                   attr:{ class: styleClass}" />

Demo JSFiddle using KO 2.0.0 and attr binding

like image 51
nemesv Avatar answered Nov 14 '22 02:11

nemesv