Skip to content

Instantly share code, notes, and snippets.

@lkostrowski
Last active April 4, 2017 18:59
Show Gist options
  • Save lkostrowski/9f7a6faa94610a3e29a714a602d02bd5 to your computer and use it in GitHub Desktop.
Save lkostrowski/9f7a6faa94610a3e29a714a602d02bd5 to your computer and use it in GitHub Desktop.
import eventHub from 'services/_eventBus';
const cartRequestUrl = '/get/basket' // Url to our backend
function getBasket( ) {
eventHub.$emit('basket:loading'); // Emit loading so components can show loaders
// Make api call
axios.get( cartRequestUrl ).then( ( response ) => {
emitBasketUpdate(response.data);
} );
}
function emitBasketUpdate() {
eventHub.$emit('basket:updated', {products: basketData}); // Emit updated basket and pass fetched data
}
// Subscribe on...
function init () {
// ...first basket page load
eventHub.$on('basketpage:loaded', () => {
getBasket();
});
// ... every upcoming basket change
eventHub.$on('basket:changed', () => {
getBasket();
});
}
export {init}; // And init in in your root js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment