Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 10th, 2012  |  syntax: CoffeeScript  |  size: 1.83 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Class definitions
  2. # -----------------------------------------------------------------------------
  3.  
  4. class Page
  5.     constructor: (@prev, @next, @content) ->
  6.  
  7.     activate : ->
  8.         $("#content").html(@content)
  9.         $("#prev").attr "href", "#/#{@prev}"
  10.         $("#next").attr "href", "#/#{@next}"
  11.  
  12. # Page init
  13. # -----------------------------------------------------------------------------
  14.  
  15. containers = []
  16.  
  17. $ ->
  18.     containers["home"] = new Page(
  19.         $("#prev").attr("href").split("#/")[1]
  20.         , $("#next").attr("href").split("#/")[1]
  21.         , $("#content").html()
  22.     )
  23.  
  24.     router = new Router
  25.         "/home": ->
  26.             containers["home"].activate()
  27.  
  28.         "/competenties/:page": (page) ->
  29.             if !containers[page]?
  30.                 $.getJSON "json/#{page}.json", (article) ->
  31.                     containers[page] = createPage article
  32.                     containers[page].activate()
  33.             else
  34.                 containers[page].activate()
  35.  
  36.     router.init()
  37.  
  38.     $(document).keydown (e) ->
  39.         if event.which in [74, 39]
  40.             window.location.href = $("#next").attr "href"
  41.         else if event.which in [75, 37]
  42.             window.location.href = $("#prev").attr "href"
  43.  
  44. # Factory method
  45. # -----------------------------------------------------------------------------
  46.  
  47. createPage = (article) ->
  48.     return new Page article.prev, article.next, renderTemplate article
  49.  
  50. # Templates
  51. # -----------------------------------------------------------------------------
  52.  
  53. renderTemplate = (article) ->
  54.     paragraphs = ("<p>#{text}</p>" for text in article.text).join ""
  55.     return  """
  56.    <div class="ten columns offset-by-three">
  57.        <article id="#{article.title}">
  58.            <h3>#{article.title}</h3>
  59.            #{paragraphs}
  60.        </article>
  61.    </div>
  62.    """