Two thoughts on this one:
- I think that you are ultimately going to have the best experience, if you try to keep your viewModel and your UI (DOM elements) as separate as possible. The view model should have almost no knowledge of the actual UI. The binding handlers should be the only link between the two. This makes it so your viewModel can stand-alone, and is portable and testable. I don't know the structure of your whole viewModel above, but it seems that you should try to get the data-viewName into the data that is the context of the binding rather than in the "data-" attribute. Maybe we could play with it in a jsFiddle?
-On the other hand, if you really want to do this, then I think that one way is to create a "wrapper" binding to the css binding.
Maybe something like (probably with a better name):
ko.bindingHandlers.cssPlus = {
init: function(element, valueAccessor, allBindingsAccessor, context) {
var options = valueAccessor();
var value = element.getAttribute(options.paramAttribute);
var result = options.action.call(context, value);
var newValueAccessor = function() {
var css = {};
css[options.css] = result;
return css;
};
ko.bindingHandlers.css.update(element, newValueAccessor, allBindingsAccessor, context);
}
};
Bind it like:
<div data-viewName="one" data-bind="cssPlus: { paramAttribute: 'data-viewName', action: someFunction, css: 'special' }">One</div>
No real reason that the binding could not be even more specific for your purposes and make more assumptions (assume 'data-viewName').