Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJs v3 - _ko_property_writers = undefined

I'm trying to get my custom binding to work with both observables and plain objects. I followed the answer in this question:

writeValueToProperty isn't available

However, if I look at the object returned if I execute the allBindingsAccessor, the property '_ko_property_writers' is undefined.

Does anyone know if this has changed at all in version 3 of knockout?

edit

Sorry I should have stated, I am trying to 'write' the value back to the model, in an observable agnostic way

like image 649
Marlon Avatar asked Nov 01 '22 08:11

Marlon


1 Answers

This was helpful for me:

ko.expressionRewriting.twoWayBindings.numericValue = true;  
ko.bindingHandlers.numericValue = {  
...  
}  

It is defined after specifying binding as two-way. So I can use something like that inside my custom binding:

ko.expressionRewriting.writeValueToProperty(underlying, allBindingsAccessor, 'numericValue', parseFloat(value)); 

writeValueToProperty is defined internally as:

writeValueToProperty: function(property, allBindings, key, value, checkIfDifferent) {
    if (!property || !ko.isObservable(property)) {
        var propWriters = allBindings.get('_ko_property_writers');
            if (propWriters && propWriters[key])
            propWriters[key](value);
    } else if (ko.isWriteableObservable(property) && (!checkIfDifferent || property.peek() !== value)) {
        property(value);
    }
}
like image 87
Maxim Avatar answered Nov 09 '22 13:11

Maxim