Last active
August 29, 2015 14:26
-
-
Save maxidr/92816444bd577a758e0b to your computer and use it in GitHub Desktop.
#mithril Route separation logic example
From https://gitter.im/lhorie/mithril.js?at=551bd24305184cd235fb971a
This file contains hidden or 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
// The limited unauthenticated routemap | |
var authRoutes = { | |
'/signup' : modules.signup, | |
'/login' : modules.login | |
}; | |
// The whole shebang | |
var appRoutes = Object.assign( { | |
'/:semantic/:app/:routes' : various.modules | |
}, authRoutes ); | |
// Called on succesful login | |
function welcome(){ | |
m.startComputation(); | |
m.route( document.body, '/profile', appRoutes ); | |
// Where they were going to in the first place | |
m.route( localStorage.getItem( 'entryPoint' ) ); | |
m.endComputation(); | |
} | |
// Run on init | |
m.route( document.body, '/', { | |
'/:path...' : { | |
controller(){ | |
// Preserve entry point | |
localStorage.setItem( 'entryPoint', m.route() ); | |
}, | |
// Wait til we've determined what's going on | |
view : mod.call( undefined, modules.loading ) | |
} | |
} ); | |
// Record of a local session | |
if( var profile = localStorage.getItem( 'profile' ) ){ | |
// Check to see if it's still ongoing server-side | |
m.request( profile.profile_uri ).then( | |
// Fork journey depending on outcome | |
() => m.route( document.body, '/', appRoutes ), | |
() => m.route( document.body, '/login', authRoutes ) | |
); | |
} | |
else { | |
// Vanilla routing | |
m.route( document.body, '/signup', authRoutes ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment