KnockoutJs v3 - _ko_property_writers = undefined

I am trying to get my own binding for working with both observable and ordinary objects. I followed the answer in this question:

writeValueToProperty is unavailable

However, if I look at the returned object, if I run allBindingsAccessor, the property "_ko_property_writers" will be undefined.

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

change

Sorry I had to say, I'm trying to “write” the value back to the model, observed in an agnostic way

+4
source share
2 answers

It helps me:

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

. - :

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

writeValueToProperty :

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);
    }
}
+2

ko.unwrap, : http://knockoutjs.com/documentation/custom-bindings.html

:

ko.bindingHandlers.slideVisible = {
    update: function(element, valueAccessor, allBindings) {
        // First get the latest data that we're bound to
        var value = valueAccessor();

        // Next, whether or not the supplied model property is observable, get its current value
        var valueUnwrapped = ko.unwrap(value);

        // Grab some more data from another binding property
        var duration = allBindings.get('slideDuration') || 400; // 400ms is default duration unless otherwise specified

        // Now manipulate the DOM element
        if (valueUnwrapped == true)
            $(element).slideDown(duration); // Make the element visible
        else
            $(element).slideUp(duration);   // Make the element invisible
    }
};

valueUnwrapped .

0

All Articles