Skip to content

Instantly share code, notes, and snippets.

@bfitch
Created August 16, 2015 14:42
Show Gist options
  • Save bfitch/bcd409d06bbe0ef05db2 to your computer and use it in GitHub Desktop.
Save bfitch/bcd409d06bbe0ef05db2 to your computer and use it in GitHub Desktop.
reactive-router: Swap rackt/history and crossroads.js for page.js
// reactive-router/index.js
'use strict';
import crossroads from 'crossroads';
import {createHistory} from 'history';
let Router = function (routes, options) {
// let isSilent = false;
// register the routes
Object.keys(routes).map(function (route) {
crossroads.addRoute(route, function() {
console.log(`${route} router callback!`);
routes[route].apply(this, arguments);
});
});
// start the router
let history = createHistory();
let unlisten = history.listen(function (location) {
console.log(location.pathname);
crossroads.parse(location.pathname);
});
unlisten();
// export functions
return {
set: function (url) {
if (window.location.pathname !== url) {
history.pushState({}, url);
}
}
// ,
// setSilent: function (url) {
// isSilent = true;
// this.set(url);
// isSilent = false;
// }
};
};
export default Router;
// main.js
import React from 'react';
import controller from './controller';
import Router from './router';
controller.signal('rootRouted', function(input, state, output) {
console.log('rootRouted signal');
console.log(arguments);
state.set('path', input.path);
});
const router = Router({
'/': controller.signals.rootRouted,
// '/': () => {
// console.log("fake rootRouted signal");
// },
'/home': controller.signals.homeRouted,
'/foo': controller.signals.fooRouted
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment