Skip to content

Instantly share code, notes, and snippets.

@twhid
Created September 20, 2016 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twhid/7de1f6f88d95e171c59496cc6258b2e2 to your computer and use it in GitHub Desktop.
Save twhid/7de1f6f88d95e171c59496cc6258b2e2 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});
}
case 'order': {
this.navigate(`/my/orders/${orderId}`, {trigger: false});
}
case 'receipt': {
this.navigate(`/my/orders/receipt/${orderId}`, {trigger: false});
}
}
});
},
// 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