Skip to content

Instantly share code, notes, and snippets.

@JamieDixon
Last active January 3, 2016 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieDixon/8422709 to your computer and use it in GitHub Desktop.
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
// 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