Permalink
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...
clarkbox committed Dec 14, 2010
1 parent 9b05e4a commit 993e6fa128c5991723316032abe12ff0cbbb9805
Showing with 2 additions and 1 deletion.
  1. +2 −1 jquery.tmpl.js
View
@@ -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

This comment has been minimized.

Show comment
Hide comment
@recalde

recalde 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.

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

This comment has been minimized.

Show comment
Hide comment
@revbones-duplicate

revbones-duplicate Apr 11, 2011

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.

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

This comment has been minimized.

Show comment
Hide comment
@fcsonline

fcsonline Jun 2, 2011

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.

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.