I’m making a portfolio and want to simply print a next & previous project link on each project detail page using DocPad (which uses Backbone collections). The code here is from the my template projects.html.eco. The @document object is the document currently being viewed.
<% for document in @getCollection('projects').toJSON() : %>
<% if document.url.indexOf('/posts') is @document.url.indexOf('/projects') + 1: %>
<a href="<%= document.url %>" class="next"><img src="/images/rt_arrow.png" alt="" /></a>
<% end %>
<% if document.url.indexOf('/posts') is @document.url.indexOf('/projects') - 1: %>
<a href="<%= document.url %>" class="previous"><img src="/images/lft_arrow.png" alt="" /></a>
<% end %>
<% end %>
Let me know if I can provide any more information!
Thank you!
Seems like there are a few things going on:
There is a combination of
/postsand/projectsbeing used in your comparison, I’m going with they are both mean to be/projectsdue to the name of the collection you are cycling.The
+ 1and- 1you current have only apply to the index position of the/projectsstring in the url, rather than the index of the actual document.For a solution for paging, there is currently this gist available, which should help for your use case. In the future, a plugin could be done up to provide the simply
next()andprev()jquery style API you’re after.