3

I know that one can always define own custom attributes for an HTML tag.

I also know that data-bind is the keyword knockout.js chose as the HTML attribute that links the data to the UI.

So far so good.

What I am curious to know is whether when I come across HTML on some website and I see that it uses the attribute data-bind, does it always imply it uses knockout.js?

<span class="chk" data-bind="visible: selectedAnswers().length > 0" />

In other words, what is the likelihood that a website using the data-bind attribute is not using knockout.js?

Community
  • 1
  • 1
datps
  • 768
  • 1
  • 6
  • 16
  • 2
    No, it's not specific to `knockout` - anyone can use it. - As far the likelihood... who knows. Look at the network tab and see if knockout is being included. – tymeJV Jan 19 '16 at 16:12
  • @tymeJV Thank you. By "network tab" do you mean the `Network` item in Firefox's Web Developer Tools? (Ctrl+Shift+Q) – datps Jan 19 '16 at 16:18
  • 2
    No it's not... I've used `data-bind` in projects not involving knockout. As long as it makes sense as to what the data is going to be used for then you can use whatever you want. – ann0nC0d3r Jan 19 '16 at 16:18
  • 1
    @datps - Yep, that's the one. – tymeJV Jan 19 '16 at 16:20
  • @ann0nC0d3r Thank you. I am happy that I asked this question, because as a novice to `knockout.js` I noticed this attribute for the first time and was wondering about that. If one of you posts your comment as answer, I will accept it. – datps Jan 19 '16 at 16:20

2 Answers2

4

No it's not... I've used data-bind in projects not involving knockout. As long as it makes sense as to what the data is going to be used for then you can use whatever string you want (within reason, see below)...

data-bind=""

data-binder=""

data-im-a-data-attribute=""

The data-* attributes consist of two parts:

1) The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix "data-"

2)The attribute value can be any string

Info obtained from here

ann0nC0d3r
  • 316
  • 1
  • 11
2

Knockout uses the data-bind attribute, but it's just a subset of the HTML5 data-* attributes. They allow for extra data to be stored in an element that don't follow standard HTML attributes.

From https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes :

data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as classList, non-standard attributes, extra properties on DOM, or setUserData.

So anyone can use an attribute named data-bind, Knockout chose to use that because it made sense for them.

dfperry
  • 2,258
  • 1
  • 14
  • 22