Skip to content

Instantly share code, notes, and snippets.

@robrichard
Last active December 17, 2016 13:13
Show Gist options
  • Save robrichard/9f3283b97938055818aee5c2756b8f26 to your computer and use it in GitHub Desktop.
Save robrichard/9f3283b97938055818aee5c2756b8f26 to your computer and use it in GitHub Desktop.
const Router = new AmpersandRouter({
// define all of the routes in our application.
// AmpersandRouter will invoke the method named when a route is
// matched on either page load or a browser back/forward click
routes: {
'/my/order/receipt/:orderId': 'loadReceipt'
'/my/order/:orderId': 'loadOrder',
'/my/orders': 'loadOrders',
},
initialize(options) {
this.store = options.store;
// subscribe to changes in our redux store. Update the URL to always match
// the data in the redux store.
options.store.subscribe(() => {
// URL “render” logic
const {pageType, orderId} = this.store.getState();
switch pageType {
case 'orders':
// trigger: false, because we are only updating the URL in response to
// current state.
this.navigate('/my/orders', {trigger: false});
break;
case 'order':
this.navigate(`/my/orders/${orderId}`, {trigger: false});
break;
case 'receipt':
this.navigate(`/my/orders/receipt/${orderId}`, {trigger: false});
break;
}
});
},
// dispatch redux actions to update the store on the new route.
loadOrders() {
this.store.dispatch({type: 'VIEW_ORDERS'});
},
loadOrder(orderId) {
this.store.dispatch({type: 'VIEW_ORDERS', orderId});
}
loadReceipt(orderId) {
this.store.dispatch({type: 'VIEW_RECEIPT', orderId});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment