Skip to content

Instantly share code, notes, and snippets.

@greim
Last active October 7, 2018 02:08
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 greim/54ac5e4b33ef711038ae8c6f974c96b2 to your computer and use it in GitHub Desktop.
Save greim/54ac5e4b33ef711038ae8c6f974c96b2 to your computer and use it in GitHub Desktop.
Motel Setup Example
/*
* Using `motel` to implement a vacancy observer.
* This happens once at app startup time.
*/
const motel = require('motel');
const vacancies = motel();
// start observing the DOM for vacancies
vacancies.connect(document.getElementById('#app'));
// pump vacancy observer output into Redux
vacancies.subscribe(action => reduxStore.dispatch(action));
// handle specific vacancy patterns
vacancies.listen('users/:id', async({ id }, dispatch) => {
dispatch({ type: 'REQUEST_USER', id });
const resp = await fetch(`/users/${id}`);
try {
const user = await resp.json();
dispatch({ type: 'RECEIVE_USER_DATA', user, id });
} catch(ex) {
dispatch({ type: 'RECEIVE_USER_ERROR', ex, id });
}
});
@greim
Copy link
Author

greim commented Dec 18, 2017

Vacancy observer pattern described here: https://medium.com/@greim/a-plan-for-data-fetching-a68d171af38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment