rniemeyer/knockout-classBindingProvider — pull request #15 by mberkom (2012-10-12 18:58:56+00:00)
Title: Routing function with prefixing

I extended the `getBindings` method to call a `getCleanPrefix` function that recurses up the DOM tree looking for nodes with a `data-class-prefix` attribute and returning the complete prefix once it hits the `

` tag or an element with a `data-class-prefix` containing "$root".

The newly generated class prefix is then passed to the binding router and prepended to the class name before it attempts to look up the binding in the bindings object.

This allows you to write HTML like this:

```html

```

instead of...

```html

```

## Technical Details

The `getPrefix` method decorates all child nodes of an element with a `data-class-prefix` attribute containing "$root". This is easiest to understand if you open up your inspector and look at the generated HTML. It adds the `data-class-prefix` attribute to all nodes, so that the second time a child element on the same branch goes to get its prefix, it doesn't have to recurse all the way up the tree again.

Knockout's `ko.domData` might be an effective way to clean things up, but I'm not sure it's the most straightforward thing to implement and might be a source of memory leaks if DOM elements are removed outside of Knockout.

Thoughts?