Skip to content

Instantly share code, notes, and snippets.

@jescalan
Created May 15, 2013 17:47
Show Gist options
  • Select an option

  • Save jescalan/5585871 to your computer and use it in GitHub Desktop.

Select an option

Save jescalan/5585871 to your computer and use it in GitHub Desktop.
You can perform some voodo magic within roots to transfer information from your dynamic content into your external javascript. This is a small sample of how one might do this, the specific example being infinite scroll functionality on a blog.
---
title: 'hello world'
---
:markdown
blah blah blah
doctype !!!
html
head
title testing roots
body
//- render out the first x posts
- number_of_posts = 5
ul
- for post, i in site.posts
- if (i < number_of_posts){
li
a(href="#{post.url}")= post.title
- }
//- create an internal API for javascript to paginate
- var remaining = site.posts.splice(0,number_of_posts)
script!= "var remaining_posts = " + JSON.stringify(remaining)
script(src='/js/templates.js')
script(src='/js/main.js')
$ ->
items_to_load = 5
loading = false
$(window).on 'scroll', ->
at_bottom = $(window).scrollTop() + $(window).height() > $(document).height() - 200
if at_bottom and not loading
loading = true
items = remaining_posts.slice(0,items_to_load).map((item) -> templates.post(item)).join('')
remaining_posts.splice(0,items_to_load)
$('ul').append(items)
loading = false
li
a(href="#{url}")= title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment