Last active
September 22, 2015 18:49
-
-
Save TylorS/98f6d0f5b577d576aa85 to your computer and use it in GitHub Desktop.
Possible router driver api
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
//...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