Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get First 3 items of backbone collection in Underscore template

{{_.each(model, function(item) { }}
<h5 style="color:#F30;">{{=item.name}}</h5> 
        {{for  (i = 0; i < item.subcollection.length; i++) {  }}  
               <li><a>{{=item.subcollection[i].split('#')[0]}}</a></li>
         {{ } }} 
{{ });}}

I have underscore template for rendering backbone model collection which contains a subcollection inside.But i need render only first 3 collection while rendering. ny help

like image 503
Juhan Avatar asked Nov 24 '25 19:11

Juhan


1 Answers

i am not sure what you are trying to do but if you want only first three to be executed set the counter value to 3 instead collection.length

 {{for(j=0;j<3;j++){ }}
<h5 style="color:#F30;">{{=model[j].name}}</h5> 
        {{for  (i = 0; i < model[j].subcollection.length; i++) {  }}  
               <li><a>{{=model[j].subcollection[i].split('#')[0]}}</a></li>
         {{ } }} 
{{ } }}

note : make sure the length is at least 3.

like image 194
StateLess Avatar answered Nov 26 '25 09:11

StateLess



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!