dual-navigator
v0.1.0
Published
Route browser applications with dualapi
Readme
dual-navigator 
dualapi domains are already like
a network of distributed HTTP servers extending into the browser.
dual-navigator furthers this pattern by using the the
window.location hash to route into a dualapi domain.
A live example is mounted at http://plediii.github.io/dual-navigator/example/index.html, for the routes at https://github.com/plediii/dual-navigator/blob/master/example/routes.js.
Using dual-navigator with dualapi
To use dual-navigator, first extend dualapi with the dual-navigator module:
var dualapi = require('dualapi').use(require('dual-navigator'));Then once you've instantiated a dualapi domain,
var domain = dualapi();you can create a navigator instance:
domain.navigator(window, {
appRoute: ['app']
, indexRoute: ['index']
, globals: {}
, cleanPage: function () {}
})The declaration that states that:
- The application routes can be reached on the domain below the
['app']route. - If no route is provided, it will default to
['app', 'index']. - Between application states, the
cleanpagefunction will be called (in this case a no-op)
Creating application routes
dual-navigator application routes are hosts which return functions
to be called when the browser navigates to a window location with the
same hash as the route:
domain.mount([app', 'index'], function (body, ctxt) {
ctxt.return(function () {
// render index application
});
});The application may optionally return a cleanup function, to be called before the next route is loaded.
domain.mount(['app', 'withcleanup'], function (body, ctxt) {
ctxt.return(function () {
// render index application
return function () {
// cleanup logic
};
});
});Optional application specific cleanup
The cleanup function may optionally return a Promise to be resolved
before continuing loading the next application. Finally, the optional
cleanup function provided during navigator instantiation will be
called, regardless of whether the app has its own cleanup function.
Normal navigator flow
The normal navigator flow is as follows:
- User navigates to a hash
- Navigator requests application
- Navigator calls previous application cleanup
- When the application cleanup resolves, navigator's
cleanup - When the cleanup complete, navigator calls the new application
See the example routes.
