Last active
January 3, 2016 06:29
-
-
Save JamieDixon/8422709 to your computer and use it in GitHub Desktop.
Want to force the update of a binding because a non observable dependency has changed? KABOOM! Nastyness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Force all properties bound to the price data binding to reevaluate | |
function forceBindingRefresh(bindingName, element) { | |
var bindings = $(element).data("bind"); | |
var koElement = ko.dataFor(element); | |
if (bindings.indexOf(bindingName) >= 0) { | |
// Lets split up the bindings so we can deal with each one | |
bindings = bindings.split(","); | |
$.each(bindings, function (index, value) { | |
// Is this the binding we're looking for? | |
if (value.indexOf(bindingName) >= 0) { | |
// Let's find out what the valueAccessor for this binding is | |
var parts = value.split(":"); | |
value = parts[1].trim().replace("()", ""); | |
var observableToUpdate = koElement; | |
var propertyParts = value.split("."); | |
// The valueAccessor has dots in it? Let's find the end of this piece of string | |
$.each(propertyParts, function (ind, val) { | |
observableToUpdate = observableToUpdate[val]; | |
}); | |
// Ahh, almost there. I wonder if we can trick this observable into knowing it's changed. | |
if (observableToUpdate.valueHasMutated) { | |
if (observableToUpdate()) { | |
observableToUpdate.valueHasMutated(); | |
} | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment