Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

hasfocus fix for focussing hidden elements (focus asynchronously) #671

Closed
wants to merge 1 commit into from

3 participants

Lars Corneliussen Steven Sanderson Vinayasimha
Lars Corneliussen

Check it out here: http://jsfiddle.net/mhrf2/

I'm not really sure if this opens up for race conditions; but it is better than now, at least.

Lars Corneliussen lcorneliussen Focus asynchronously if element is hidden
I'm not really sure if this opens up for race conditions; but it is better than now, at least.
Check it out here: http://jsfiddle.net/mhrf2/
2ad7986
Lars Corneliussen

Candidate for superseeding #355

Lars Corneliussen

Hm. Doesn't fix my real problem. binding and element seem to go out of sync on my end. Element is not focussed, but the observable is true... computed dependents are not updated...

Lars Corneliussen

Ok. My problem is this:
#672

Current master breaks backwards-compat - click to focus does not work.

Steven Sanderson

Hm. Doesn't fix my real problem. binding and element seem to go out of sync on my end.

Does this mean you are no longer suggesting this solution?

In general, we try hard not to do stuff asynchronously within Knockout (except when you explicitly opt into it, as in the throttle extender). The reason is that once something starts happening asynchronously and you have no way of knowing exactly when, then it quickly becomes impossible to chain together sequences of operations except by adding yet more setTimeout hacks.

I'll close #671 now and leave #355 open to represent the basic concern about focusing hidden elements. Please let me know if you disagree!

Lars Corneliussen

did the last comment on #355

the issue is still current - i still have a fork running :(

Vinayasimha

This code worked for me:

var koHasFocusUpdateFuction = ko.bindingHandlers.hasfocus.update;
ko.bindingHandlers.hasfocus.update = function(element, valueAccessor) {
if ($(element).is(":visible"))
koHasFocusUpdateFuction(element, valueAccessor);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 15, 2012
  1. Lars Corneliussen

    Focus asynchronously if element is hidden

    lcorneliussen authored
    I'm not really sure if this opens up for race conditions; but it is better than now, at least.
    Check it out here: http://jsfiddle.net/mhrf2/
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 1 deletion.
  1. +6 −1 src/binding/defaultBindings/hasfocus.js
7 src/binding/defaultBindings/hasfocus.js
View
@@ -28,7 +28,12 @@ ko.bindingHandlers['hasfocus'] = {
'update': function(element, valueAccessor) {
if (!element[hasfocusUpdatingProperty]) {
var value = ko.utils.unwrapObservable(valueAccessor());
- value ? element.focus() : element.blur();
+ var doUpdate = function(){value ? element.focus() : element.blur()};
+ if (element.offsetWidth === 0) {
+ setTimeout(doUpdate);
+ } else {
+ doUpdate();
+ }
ko.dependencyDetection.ignore(ko.utils.triggerEvent, null, [element, value ? "focusin" : "focusout"]); // For IE, which doesn't reliably fire "focus" or "blur" events synchronously
}
}
Something went wrong with that request. Please try again.