UNPKG

todomvc

Version:

> Helping you select an MV\* framework

51 lines (44 loc) 1.53 kB
/*global define */ define([ 'knockout', 'config/global' ], function (ko, g) { 'use strict'; // a custom binding to handle the enter key (could go in a separate library) ko.bindingHandlers.enterKey = { init: function (element, valueAccessor, allBindingsAccessor, data, bindingContext) { var wrappedHandler; var newValueAccessor; // wrap the handler with a check for the enter key wrappedHandler = function (data, event) { if (event.keyCode === g.ENTER_KEY) { valueAccessor().call(this, data, event); } }; // create a valueAccessor with the options that we would want to pass to the event binding newValueAccessor = function () { return { keyup: wrappedHandler }; }; // call the real event binding's init function ko.bindingHandlers.event.init(element, newValueAccessor, allBindingsAccessor, data, bindingContext); } }; // wrapper to hasfocus that also selects text and applies focus async ko.bindingHandlers.selectAndFocus = { init: function (element, valueAccessor, allBindingsAccessor) { ko.bindingHandlers.hasfocus.init(element, valueAccessor, allBindingsAccessor); ko.utils.registerEventHandler(element, 'focus', function () { element.focus(); }); }, 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); } }; });