Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adds an "$index" property (similar to the each tag) to the data item …

…passed to nested templates using the {{tmpl}} tag

fixes: https://github.com/jquery/jquery-tmpl/issues#issue/28
  • Loading branch information...
commit 993e6fa128c5991723316032abe12ff0cbbb9805 1 parent 9b05e4a
Clark A authored December 13, 2010

Showing 1 changed file with 2 additions and 1 deletion. Show diff stats Hide diff stats

  1. 3  jquery.tmpl.js
3  jquery.tmpl.js
@@ -144,7 +144,8 @@
144 144
 				updateWrapped( options, options.wrapped );
145 145
 			}
146 146
 			ret = jQuery.isArray( data ) ? 
147  
-				jQuery.map( data, function( dataItem ) {
  147
+				jQuery.map( data, function( dataItem, index ) {
  148
+					if(dataItem){dataItem.$index = index;}
148 149
 					return dataItem ? newTmplItem( options, parentItem, tmpl, dataItem ) : null;
149 150
 				}) :
150 151
 				[ newTmplItem( options, parentItem, tmpl, data ) ];

3 notes on commit 993e6fa

Rob Recalde

Thanks! this snippet helped me get my nested templates with jQuery UI tabs working, since they need the index in the div id. You the man.

revbones-duplicate

Awesome! The lack of $index was one of my chief frustrations with this library! Especially after the MS AJAX Library (that got scrubbed for this turd) was so comparatively robust.

Ferran Basora

I think that is a good improvement but could be better if you don't modify the original dataItem. Think about modify the newTmplItem:

return dataItem ? newTmplItem( options, parentItem, tmpl, dataItem, index ) : null;

If you print your datasource after the template render, all items includes the $index attribute, and this modification provoke an error in other places in my application.

Please sign in to comment.
Something went wrong with that request. Please try again.