Browse files

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

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

  • Loading branch information...
1 parent 9b05e4a commit 993e6fa128c5991723316032abe12ff0cbbb9805 @clarkbox committed Dec 14, 2010
Showing with 2 additions and 1 deletion.
  1. +2 −1 jquery.tmpl.js
@@ -144,7 +144,8 @@
updateWrapped( options, options.wrapped );
ret = jQuery.isArray( data ) ?
- data, function( dataItem ) {
+ data, function( dataItem, index ) {
+ if(dataItem){dataItem.$index = index;}
return dataItem ? newTmplItem( options, parentItem, tmpl, dataItem ) : null;
}) :
[ newTmplItem( options, parentItem, tmpl, data ) ];

3 comments on commit 993e6fa

recalde commented on 993e6fa Jan 6, 2011

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.


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.


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.