Created
September 20, 2016 13:32
-
-
Save twhid/7de1f6f88d95e171c59496cc6258b2e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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