knockoutjs:Example 1: 親が真なら子・孫のバインディングは無効:The custom binding Control descendant bindings

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjs:Example 1: 親が真なら子・孫のバインディングは無効:The custom binding Control descendant bindings</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 1: 親が真なら子・孫のバインディングは無効:The custom binding Control descendant bindings</h1>
  11. <div data-bind="allowBindings: true">
  12.     <!-- This will display Replacement, because bindings are applied -->
  13.     <div data-bind="text: 'Replacement'">Original</div>
  14. </div>
  15.  
  16. <div data-bind="allowBindings: false">
  17.     <!-- This will display Original, because bindings are not applied -->
  18.     <div data-bind="text: 'Replacement'">Original</div>
  19. </div>
  20. <script type="text/javascript">
  21. ko.bindingHandlers.allowBindings = {
  22.   init: function(elem, valueAccessor) {
  23.     // Let bindings proceed as normal *only if* my value is false
  24.     var shouldAllowBindings = ko.utils.unwrapObservable(valueAccessor());
  25.     return { controlsDescendantBindings: !shouldAllowBindings };
  26.   }
  27. };
  28. var viewModel = {};
  29. ko.applyBindings(viewModel);
  30. </script>
  31. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  32. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  33. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  34. </div>
  35. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  36. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  37. </div>
  38. </body>
  39. </html>