Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reflux = require('reflux')
module.exports =
navigate: Reflux.createAction()
Router = require('routr')
paths =
'login': "/login"
'resetPassword': "/reset-password"
'contacts': "/contacts"
'contacts.add': "/contacts/new"
'contacts.importFromFacebook': "/contacts/import/facebook"
'contacts.importFromLinkedin': "/contacts/import/linkedin"
'contacts.details': "/contacts/:contactSlug"
'settings': "/settings"
'settings.accountDetails': "/settings/account-details"
'settings.linkedAccounts': "/settings/linked-accounts"
'settings.invoices': "/settings/invoices"
'settings.invoices.details': "/settings/invoices/:invoiceId"
routes = {}
for name, path of paths
routes[name] =
path: path
method: "get"
module.exports = new Router routes
Reflux = require('reflux')
routes = require('../routes')
RouteActions = require('../actions/RouteActions')
getLocation = ->
window.location.hash.substr(1)
getCurrentRoute = ->
routes.getRoute(getLocation()) ? {name: '404'}
ensureSlash = ->
path = getLocation()
if path.charAt(0) == '/'
true
else
windowPath = window.location.pathname + window.location.search
window.location.replace(windowPath + '#/' + path)
false
onHashChange = ->
if ensureSlash()
RouteActions.navigate.trigger(getCurrentRoute())
#
# Router state
#
# Make sure the path is correct before setting the intial route
ensureSlash()
# Set initial route
_currentRoute = getCurrentRoute()
RouteStore = Reflux.createStore
listenables: RouteActions
onNavigate: (route) ->
_currentRoute = route
@trigger()
getCurrentRoute: ->
_currentRoute
#
# Listen for changes
#
if window.addEventListener
window.addEventListener('hashchange', onHashChange, false)
else
window.attachEvent('onhashchange', onHashChange)
module.exports = RouteStore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment