Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When your viewModel is not accessible globally, there are a couple of options.</p> <p>First, you can pass any relevant methods using the <code>templateOptions</code> parameter to the template binding.</p> <p>It would look like (also note that a static template name should be in quotes):</p> <pre><code>data-bind="template: {name: 'testTemplate', foreach: items, templateOptions: { vmMethod: methodFromMainViewModel } }" </code></pre> <p>Then, inside of the template <code>vmMethod</code> would be available as <code>$item.vmMethod</code>. If you are using templateOptions as the last parameter, then make sure that there is a space between your braces <code>{ {</code> or jQuery templates tries to parse it as its own.</p> <p>So, you can bind to it like:</p> <pre><code>&lt;img src="icon.png" data-bind="click: $item.vmMethod" /&gt; </code></pre> <p>The other option is to put a method and a reference to anything relevant from the view model on each item. It looks like you were exploring that option.</p> <p>Finally, in KO 1.3 (hopefully out in September and in beta soon) there will be a nice way to use something like jQuery's live/delegate functionality and connect it with your viewModel (like in this sample: <a href="http://jsfiddle.net/rniemeyer/5wAYY/">http://jsfiddle.net/rniemeyer/5wAYY/</a>)</p> <p>Also, the "Avoiding anonymous functions in event bindings" section of this <a href="http://www.knockmeout.net/2011/08/simplifying-and-cleaning-up-views-in.html">post</a> might be helpful to you as well. If you are looking for a sample of editing in place using a dynamically chosen template, then this <a href="http://www.knockmeout.net/2011/03/quick-tip-dynamically-changing.html">post</a> might help.</p>
 

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