Skip to content

Instantly share code, notes, and snippets.

@wiljanslofstra
Last active December 16, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiljanslofstra/5450456 to your computer and use it in GitHub Desktop.
Save wiljanslofstra/5450456 to your computer and use it in GitHub Desktop.
Router with director
define(['director', 'listView','mapView','detailView'], (director, list, map, detail) ->
init = () ->
# Setup Routes
routes =
'/map': mapView
'/detail': detailView
'/list': listView
'/favs': favView
'/': listView
# Initialize router object
router = Router(routes)
# Configure to route all through the allRoutes function
router.configure({
on: allRoutes
})
# Finally initialize it
router.init()
# Initialize appropiate views
listView = () -> list.init()
mapView = () -> map.init()
detailView = () -> detail.init()
favView = () ->
console.log 'fav view'
# All routes go through here, and show hide certain blocks
allRoutes = () ->
route = window.location.hash.slice(2)
sections = document.getElementsByTagName('section')
# If root (/#/), show listView
if route is ''
route = 'list'
# Loop through sections and hide all not matching ones
for el in sections
# Get attribute data-route from current element
attrs = el.getAttribute('data-route')
# See if it fits the route given
if attrs is route
el.style.display = "block"
else
el.style.display = "none"
init()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment