Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make a recursive jQuery template (tmpl) availbe outside of my plugin?
    primarykey
    data
    text
    <p>I have a Json object that I'm dumping into a jQuery template (tmpl). It needs to be recursive because the Json object can be n-levels deep. This code works. However, if I wrap it in a function (it belongs in a jQuery plugin i'm writing), it stops working. To see what I mean simply uncomment the function wrapper in the Javascript.</p> <p>Play with it: <a href="http://jsfiddle.net/dbme/tkdZg/6/" rel="nofollow">http://jsfiddle.net/dbme/tkdZg/6/</a></p> <p>HTML: </p> <pre><code>&lt;script id="evntTemplate" type="text/x-jquery-tmpl"&gt; {{if data.identifiers}} &lt;div id="${data.identifiers.id}" class="bsa-event"&gt; type: ${data.identifiers.type}&lt;br /&gt; id: ${data.identifiers.id}&lt;br /&gt; {{if children}} {{each(i, child) children}} &lt;blockquote&gt; &lt;p&gt; {{if children}} {{tmpl(children) "evntTemplate"}} {{/if}} &lt;/p&gt; &lt;/blockquote&gt; {{/each}} {{/if}} &lt;/div&gt; {{/if}} &lt;/script&gt; &lt;div id="eventList"&gt;&lt;/div&gt; </code></pre> <p>Javascript:</p> <pre><code>//(function ($) { // $.fn.SomePlugin = function() { var movies = { "data": { "identifiers":{ "type":0, "id":"makeunique_907827h" } }, "children": { "data": { "identifiers": { "type":1, "id":"makeunique_716786g" } }, "children": { "data": { "identifiers": { "type":1, "id":"makeunique_234355g" } } } } }; /* Render the template with the data */ var evntTemplate = $( "#evntTemplate" ).template( "evntTemplate" ); $.tmpl(evntTemplate, movies).appendTo("#eventList"); //}; //}( jQuery )); </code></pre> <p>More info: I'm using RequireJS to load the files and kick off the whole process. I think that's what's killing the scope. Here's the code for that:</p> <pre><code>require(["jquery", "jquery.tmpl", "jquery.someplugin"], function($) { $(function() { $('body').SomePlugin(); }); }); </code></pre>
    singulars
    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.
 

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