knockoutjs:Example 2: JqueryのslideUp/slideDownを組み込む:The custom binding

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjs:Example 2: 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 2: 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.   update: function(element, valueAccessor, allBindingsAccessor)
  16.   {
  17.     // First get the latest data that we're bound to
  18.     var value = valueAccessor(), allBindings = allBindingsAccessor();
  19.     // Next, whether or not the supplied model property is observable, get its current value
  20.     var valueUnwrapped = ko.utils.unwrapObservable(value);
  21.     // Grab some more data from another binding property
  22.     var duration = allBindings.slideDuration || 400; // 400ms is default duration unless otherwise specified
  23.     // Now manipulate the DOM element
  24.     if (valueUnwrapped == true)
  25.     {
  26.       $(element).slideDown(duration); // Make the element visible
  27.     }
  28.     else
  29.     {
  30.       $(element).slideUp(duration);   // Make the element invisible
  31.     }
  32.   }
  33. };
  34. var viewModel = {
  35.   giftWrap: ko.observable(true)
  36. };
  37. ko.applyBindings(viewModel);
  38. </script>
  39. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  40. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  41. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  42. </div>
  43. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  44. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  45. </div>
  46. </body>
  47. </html>