Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The thing is that knockout will always use the value after unwrapping it. If it happens to be an observable, you'll lose those sub-properties. You'll have to rewrap your observable into another object so you don't lose it as you have already found.</p> <p>A nice way you can wrap this up would be to create a function for subscribables (or any of the more derived types) which will do this rewrapping. You can either tack on all individual metadata onto this rewrapped object or pack them into their own separate object. Your code can be elegant again.</p> <pre><code>var buildSelection = function (choices, Parent) { return _(ko.observable()).extend({ // add the metadata to a 'meta' object meta: { choices: choices, availableChoices: ko.computed(function () { if (!Parent) return choices; if (!Parent()) return []; return _(choices).where({ ParentID: Parent().ID }); }) } }); } ko.subscribable.fn.templateData = function (metaName) { return { // access the value through 'value' value: this, // access the metadata through 'meta' meta: this[metaName || 'meta'] // meta property may be overridden }; } </code></pre> <p>Then in your bindings, call this function to create the rewrapped object. Just remember to adjust your bindings in your template.</p> <pre><code>&lt;script id="Selection" type="text/html"&gt; &lt;select data-bind=" options: meta.availableChoices, optionsText: 'Value', value: value, optionsCaption: 'Select One', enable: meta.availableChoices().length "&gt;&lt;/select&gt; &lt;/script&gt; &lt;!-- ko template: { 'name': 'Selection', 'data': Level1.templateData() } --&gt;&lt;!-- /ko --&gt; &lt;!-- ko template: { 'name': 'Selection', 'data': Level2.templateData() } --&gt;&lt;!-- /ko --&gt; &lt;!-- ko template: { 'name': 'Selection', 'data': Level3.templateData() } --&gt;&lt;!-- /ko --&gt; </code></pre> <p><a href="http://jsfiddle.net/jeff1203/UFVYg/9/" rel="nofollow">Updated fiddle</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload