0
votes

I have a custom knockout binding, e.g. like this:

ko.bindingHandlers.yourBindingName = {
    init: function(element, valueAccessor, allBindingsAccessor,
                   viewModel, bindingContext)
    {
        // how to access the value-part of the data-bind="name:value" attribute?
    },
};

and:

<div data-bind="yourBindingName: someValue"> </div>

Is it possible to access the value part of the data-bind attribute (i.e. "someValue" in the above example) from the init-function?


Update: Just to clarify what I mean:

  • the data-bind attribute of the div has a name and value part, separated by a colon, e.g. "yourBindingName: someValue"
  • the name-part specifies which binding to call
  • I'd like to access the value-part inside my binding and use it like a parameter
2
I've got to be honest here, my question is why do you want to do this? - Paul Manzotti
Would you mind changing the title to something like "access the name of the binding target" or something other than "value", its very confusing. - Kyeotic
@Tyrsius: I could change the title, but I'm not sure if it would be correct. According to the KO documentation, the second part of the data-bind attribute is called "value". - M4N
@M4N That's true, but its still confusing. An inaccurate but less ambiguous term may still be clearer to someone reading just the question title. - Kyeotic
@M4N Also, does my answer not work for you? - Kyeotic

2 Answers

0
votes

If I understand you correctly, then the data sent into the custom binding handler is the valueAccessor, as an observable. So if you want the value, then do:

var value = valueAccessor();
0
votes

I have no idea why you would ever want to do this, but here you go:

ko.bindingHandlers.reflector = {
    init: function(element, valueAccessor, allBindingsAccessor,
                   viewModel, bindingContext)
    {
        var reflected;
        var binding = valueAccessor();
        for (var prop in viewModel) {
            if (viewModel[prop] == binding) {
                reflected = prop;
                break;
            }
        }
        ko.bindingHandlers.text.update(element, function(){ return reflected});
    },
};

and in a fiddle