
Untitled
By: a guest on
Jun 10th, 2012 | syntax:
CoffeeScript | size: 1.83 KB | hits: 30 | expires: Never
# Class definitions
# -----------------------------------------------------------------------------
class Page
constructor: (@prev, @next, @content) ->
activate : ->
$("#content").html(@content)
$("#prev").attr "href", "#/#{@prev}"
$("#next").attr "href", "#/#{@next}"
# Page init
# -----------------------------------------------------------------------------
containers = []
$ ->
containers["home"] = new Page(
$("#prev").attr("href").split("#/")[1]
, $("#next").attr("href").split("#/")[1]
, $("#content").html()
)
router = new Router
"/home": ->
containers["home"].activate()
"/competenties/:page": (page) ->
if !containers[page]?
$.getJSON "json/#{page}.json", (article) ->
containers[page] = createPage article
containers[page].activate()
else
containers[page].activate()
router.init()
$(document).keydown (e) ->
if event.which in [74, 39]
window.location.href = $("#next").attr "href"
else if event.which in [75, 37]
window.location.href = $("#prev").attr "href"
# Factory method
# -----------------------------------------------------------------------------
createPage = (article) ->
return new Page article.prev, article.next, renderTemplate article
# Templates
# -----------------------------------------------------------------------------
renderTemplate = (article) ->
paragraphs = ("<p>#{text}</p>" for text in article.text).join ""
return """
<div class="ten columns offset-by-three">
<article id="#{article.title}">
<h3>#{article.title}</h3>
#{paragraphs}
</article>
</div>
"""