Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@clarkbox authored
Showing with 2 additions and 1 deletion.
  1. +2 −1  jquery.tmpl.js
View
3  jquery.tmpl.js
@@ -144,7 +144,8 @@
updateWrapped( options, options.wrapped );
}
ret = jQuery.isArray( data ) ?
- jQuery.map( data, function( dataItem ) {
+ jQuery.map( 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

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.

@fcsonline

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.