Skip to content

Instantly share code, notes, and snippets.

@TylorS
Last active Sep 22, 2015
Embed
What would you like to do?
Possible router driver api
//...imports...
// Declare routes and their associated action
// route:action
const routeDeclaration = {
'/users': {
'/' : 123,
':userId': 456
':userId/profile': 789
}
};
function intent({Router}) {
/*
Router is an observable with the current location.
*/
/* Navigate to /users/123*/
let routeHandler$ = Router.map(location => {
let {path, value} = switchPath(location, routeDeclaration);
return {
path, value
}
}
);
//...finish creating intent...
}
function model(actions) {
//...creating state...
}
function view(state) {
//...creating view...
}
function main(responses) {
let actions = intent(responses);
let state = model(actions);
let view$ = view(state);
/*
url$ is an observable which holds the current URL
you would like to the router driver to *Set*
*/
let url$ = state.url$
/*
url$.map(url => {
// url === '/user/123'
})
*/
return {
// The router would filter the stream and if it is an object it is a declaration
// If it is a string, this is the route to set.
Router: url$
//...other drivers...
}
}
let drivers = {
/*
makeRouterDriver() would take options for the router to handle.
*/
Router: makeRouterDriver({
/*
The default to hashBang will be false
Fallback to hashBang mode is determined by browser support
Can be set to true and alway be enabled
*/
hashBang: true,
/*
The router can be set to run from a prefix, so it can update the addressbar properly.
In this case the router is running on the '/home' prefix of your site like this
www.example.com/home/#!/user/123
*/
prefix: '/home'
})
}
run(main, drivers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment