Last active
December 16, 2015 14:39
-
-
Save wiljanslofstra/5450456 to your computer and use it in GitHub Desktop.
Router with director
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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