Skip to content

Commit

Permalink
Added comments and slight tweak of code style purely to aid my own lo…
Browse files Browse the repository at this point in the history
…ng-term understanding of this. Core mechanism not affected.
  • Loading branch information
SteveSanderson committed Feb 26, 2012
1 parent 3e63a39 commit 26c9303
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/binding/defaultBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,20 @@ ko.bindingHandlers['value'] = {
var elementValue = ko.selectExtensions.readValue(element);
ko.jsonExpressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'value', elementValue);
}
function asyncValueUpdateHandler() {
setTimeout(valueUpdateHandler);
}
function ieAutoCompleteHackNeeded() {
return ko.utils.ieVersion && element.tagName.toLowerCase() == "input" && element.type == "text"
&& element.autocomplete != "off" && (!element.form || element.form.autocomplete != "off");
}

if (ieAutoCompleteHackNeeded() && ko.utils.arrayIndexOf(eventsToCatch, "propertychange") == -1) {
var propertyChanged = false, originalValueUpdateHandler = valueUpdateHandler;
valueUpdateHandler = function() {
if (propertyChanged) {
originalValueUpdateHandler();
propertyChanged = false;
// Workaround for https://github.com/SteveSanderson/knockout/issues/122
// IE doesn't fire "change" events on textboxes if the user selects a value from its autocomplete list
var ieAutoCompleteHackNeeded = ko.utils.ieVersion && element.tagName.toLowerCase() == "input" && element.type == "text"
&& element.autocomplete != "off" && (!element.form || element.form.autocomplete != "off");
if (ieAutoCompleteHackNeeded && ko.utils.arrayIndexOf(eventsToCatch, "propertychange") == -1) {
var propertyChangedFired = false;
ko.utils.registerEventHandler(element, "propertychange", function () { propertyChangedFired = true });
ko.utils.registerEventHandler(element, "blur", function() {
if (propertyChangedFired) {
propertyChangedFired = false;
valueUpdateHandler();
}
};
ko.utils.registerEventHandler(element, "propertychange", function () { propertyChanged = true; });
ko.utils.registerEventHandler(element, "blur", valueUpdateHandler);
});
}

ko.utils.arrayForEach(eventsToCatch, function(eventName) {
Expand All @@ -159,7 +155,7 @@ ko.bindingHandlers['value'] = {
// (otherwise, ko.selectExtensions.readValue(this) will receive the control's value *before* the key event)
var handler = valueUpdateHandler;
if (ko.utils.stringStartsWith(eventName, "after")) {
handler = asyncValueUpdateHandler;
handler = function() { setTimeout(valueUpdateHandler, 0) };
eventName = eventName.substring("after".length);
}
ko.utils.registerEventHandler(element, eventName, handler);
Expand Down
9 changes: 9 additions & 0 deletions src/binding/jsonExpressionRewriting.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ ko.jsonExpressionRewriting = (function () {
return false;
},

// Internal, private KO utility for updating model properties from within bindings
// property: If the property being updated is (or might be) an observable, pass it here
// If it turns out to be a writable observable, it will be written to directly
// allBindingsAccessor: All bindings in the current execution context.
// This will be searched for a '_ko_property_writers' property in case you're writing to a non-observable
// key: The key identifying the property to be written. Example: for { hasFocus: myValue }, write to 'myValue' by specifying the key 'hasFocus'
// value: The value to be written
// checkIfDifferent: If true, and if the property being written is a writable observable, the value will only be written if
// it is !== existing value on that writable observable
writeValueToProperty: function(property, allBindingsAccessor, key, value, checkIfDifferent) {
if (!property || !ko.isWriteableObservable(property)) {
var propWriters = allBindingsAccessor()['_ko_property_writers'];
Expand Down

0 comments on commit 26c9303

Please sign in to comment.