knockoutjs:Example 3: JqueryのslideUp/slideDownを組み込む:初期値設定:The custom binding

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjs:Example 3: JqueryのslideUp/slideDownを組み込む:初期値設定:The custom binding</title>
  6. <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
  7. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  8. </head>
  9. <body>
  10. <h1>Example 3: JqueryのslideUp/slideDownを組み込む:初期値設定:The custom binding</h1>
  11. <div data-bind="slideVisible: giftWrap, slideDuration:600">You have selected the option</div>
  12. <label><input type="checkbox" data-bind="checked: giftWrap" /> Gift wrap</label>
  13. <script type="text/javascript">
  14. ko.bindingHandlers.slideVisible = {
  15.   init: function(element, valueAccessor) {
  16.     var value = ko.utils.unwrapObservable(valueAccessor()); // Get the current value of the current property we're bound to
  17.     $(element).toggle(value); // jQuery will hide/show the element depending on whether "value" or true or false
  18.   },
  19.   update: function(element, valueAccessor, allBindingsAccessor)
  20.   {
  21.     // First get the latest data that we're bound to
  22.     var value = valueAccessor(), allBindings = allBindingsAccessor();
  23.     // Next, whether or not the supplied model property is observable, get its current value
  24.     var valueUnwrapped = ko.utils.unwrapObservable(value);
  25.     // Grab some more data from another binding property
  26.     var duration = allBindings.slideDuration || 400; // 400ms is default duration unless otherwise specified
  27.     // Now manipulate the DOM element
  28.     if (valueUnwrapped == true)
  29.     {
  30.       $(element).slideDown(duration); // Make the element visible
  31.     }
  32.     else
  33.     {
  34.       $(element).slideUp(duration);   // Make the element invisible
  35.     }
  36.   }
  37. };
  38. var viewModel = {
  39.   giftWrap: ko.observable()
  40. };
  41. ko.applyBindings(viewModel);
  42. </script>
  43. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  44. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  45. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  46. </div>
  47. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  48. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  49. </div>
  50. </body>
  51. </html>