Skip to content

Commit

Permalink
Merge branch '698-hasfocus-focus-getting-set-back'
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Dec 9, 2012
2 parents b22cd76 + e010ecd commit 8935de4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/binding/defaultBindings/hasfocus.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var hasfocusUpdatingProperty = '__ko_hasfocusUpdating';
var hasfocusLastValue = '__ko_hasfocusLastValue';
ko.bindingHandlers['hasfocus'] = {
'init': function(element, valueAccessor, allBindingsAccessor) {
var handleElementFocusChange = function(isFocused) {
Expand All @@ -15,6 +16,9 @@ ko.bindingHandlers['hasfocus'] = {
}
var modelValue = valueAccessor();
ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'hasfocus', isFocused, true);

//cache the latest value, so we can avoid unnecessarily calling focus/blur in the update function
element[hasfocusLastValue] = isFocused;
element[hasfocusUpdatingProperty] = false;
};
var handleElementFocusIn = handleElementFocusChange.bind(null, true);
Expand All @@ -26,8 +30,8 @@ ko.bindingHandlers['hasfocus'] = {
ko.utils.registerEventHandler(element, "focusout", handleElementFocusOut); // For IE
},
'update': function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (!element[hasfocusUpdatingProperty]) {
var value = !!ko.utils.unwrapObservable(valueAccessor()); //force boolean to compare with last value
if (!element[hasfocusUpdatingProperty] && element[hasfocusLastValue] !== value) {
value ? element.focus() : element.blur();
ko.dependencyDetection.ignore(ko.utils.triggerEvent, null, [element, value ? "focusin" : "focusout"]); // For IE, which doesn't reliably fire "focus" or "blur" events synchronously
}
Expand Down

0 comments on commit 8935de4

Please sign in to comment.