Commit 53d5a335 authored by Ryan Niemeyer's avatar Ryan Niemeyer

apply focus() asynchronously to prevent issues with element not being visible yet.

parent 66e495cb
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
<a class="destroy" href="#" data-bind="click: $root.remove"></a> <a class="destroy" href="#" data-bind="click: $root.remove"></a>
</div> </div>
<input class="edit" type="text" <input class="edit" type="text"
data-bind="value: content, valueUpdate: 'afterkeydown', enterKey: stopEditing, hasfocus: editing, select: true, event: { blur: stopEditing }"/> data-bind="value: content, valueUpdate: 'afterkeydown', enterKey: stopEditing, selectAndFocus: editing, event: { blur: stopEditing }"/>
</li> </li>
</ul> </ul>
</section> </section>
......
...@@ -28,14 +28,20 @@ ...@@ -28,14 +28,20 @@
} }
}; };
//select text when element is focused //wrapper to hasfocus that also selects text and applies focus async
ko.bindingHandlers.select = { ko.bindingHandlers.selectAndFocus = {
init:function (element) { init:function (element, valueAccessor, allBindingsAccessor) {
var handler = function () { ko.bindingHandlers.hasfocus.init(element, valueAccessor, allBindingsAccessor);
ko.utils.registerEventHandler(element, "focus", function () {
element.select(); element.select();
}; });
},
ko.utils.registerEventHandler(element, "focus", handler); update:function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); //for dependency
//ensure that element is visible before trying to focus
setTimeout(function () {
ko.bindingHandlers.hasfocus.update(element, valueAccessor);
}, 0);
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment