knockoutjs:Example 3: 親の親の値を使う(?):The custom binding Control descendant bindings
- <!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="UTF-8">
- <title>knockoutjs:Example 3: 親の親の値を使う(?):The custom binding Control descendant bindings</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 3: 親の親の値を使う(?):The custom binding Control descendant bindings</h1>
- <div data-bind="withProperties: { displayMode: 'twoColumn' }">
- The outer display mode is <span data-bind="text: displayMode"></span>.
- <div data-bind="withProperties: { displayMode: 'doubleWidth' }">
- The inner display mode is <span data-bind="text: displayMode"></span>, but I haven't forgotten
- that the outer display mode is <span data-bind="text: $parentContext.displayMode"></span>.
- </div>
- </div>
- <script type="text/javascript">
- ko.bindingHandlers.withProperties = {
- init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
- // Make a modified binding context, with a extra properties, and apply it to descendant elements
- var newProperties = valueAccessor(),
- childBindingContext = bindingContext.createChildContext(viewModel);
- ko.utils.extend(childBindingContext, newProperties);
- ko.applyBindingsToDescendants(childBindingContext, element);
-
- // Also tell KO *not* to bind the descendants itself, otherwise they will be bound twice
- return { controlsDescendantBindings: true };
- }
- };
- var viewModel = {};
- 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>