knockoutjs:Example 4: hasFocusbバインディングの拡張:The custom binding
- <!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="UTF-8">
- <title>knockoutjs:Example 4: hasFocusbバインディングの拡張:The custom binding</title>
- <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
- <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
- </head>
- <body>
- <h1>Example 4: hasFocusbバインディングの拡張:The custom binding</h1>
- <p>Name: <input data-bind="hasFocus: editingName" /></p>
-
- <!-- Showing that we can both read and write the focus state -->
- <div data-bind="visible: editingName">You're editing the name</div>
- <button data-bind="enable: !editingName(), click:function() { editingName(true) }">Edit name</button>
- <script type="text/javascript">
- ko.bindingHandlers.hasFocus = {
- init: function(element, valueAccessor)
- {
- $(element).focus(function()
- {
- var value = valueAccessor();
- value(true);
- });
- $(element).blur(function()
- {
- var value = valueAccessor();
- value(false);
- });
- },
- update: function(element, valueAccessor)
- {
- var value = valueAccessor();
- if (ko.utils.unwrapObservable(value))
- element.focus();
- else
- element.blur();
- }
- };
- var viewModel = {
- editingName: ko.observable()
- };
- ko.applyBindings(viewModel);
- </script>
- <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
- <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
- <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
- </div>
- <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
- Copyright © 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
- </div>
- </body>
- </html>