rniemeyer/knockout-classBindingProvider — pull request #8 by drosen0 (2012-09-11 19:01:29+00:00)
Title: Suggestion: Routing function

In #5, you had suggested a fallback function to call if a binding cannot be found. This pull is a little different but gives the same capability and is more powerful. You can optionally pass in a function instead of an object for the `bindings` argument. This function takes the class name and classes array as arguments, and returns a binding accessor. This makes a few things possible:

* Organize bindings however you like. For example, you could use `.` to traverse subobjects:

```html

...

```

```js
var bindings = {
binding1: ...,
twitter: {
binding2: ...
}
};

var bindingRouter = function(className) {
var result, segments, i;
result = bindings;
segments = path.split('.');
for (i = 0; i < segments.length; i++) {
result = result[segments[i]];
if (!result) return;
}
return result[className];
};

ko.bindingProvider.instance = new ko.classBindingProvider(bindingRouter);
```

* Or you could do the equivalent using a namespace parameter:

```html

...

```

```js
var bindingRouter = function(className, classes) {
var namespace, paramName = "namespace:";
namespace = ko.utils.arrayFirst(classes, function(clas) {
return clas.lastIndexOf(paramName, 0) === 0; // check whether clas starts with paramName
});
if (namespace) {
namespace = namespace.substring(paramName.length);
// get the binding object for namespace
}
};
```

* Suppress or alter the selection of a class binding based on the presence of another class:

```html

...

```

* Log troubleshooting info to the console:

```js
var bindingRouter = function(className, classes) {
var result = bindings[className];
if (result) {
console.log("Found binding '" + className + "'")
}
else {
console.log("Missing binding '" + className + "' from classes array [" + classes.join + "]");
}
return result;
};
```