Skip to content

Instantly share code, notes, and snippets.

@Yomguithereal
Last active October 26, 2015 16:24
Show Gist options
  • Save Yomguithereal/b77e455766fd489fa54d to your computer and use it in GitHub Desktop.
Save Yomguithereal/b77e455766fd489fa54d to your computer and use it in GitHub Desktop.
Baobab fetcher
const client = new APIClient(); // whatever. I use https://github.com/Yomguithereal/djax-client
const store = new LocalStore(); // whatever. A localStorage abstraction
// A tree
const tree = new Baobab({
fetchingUsers: false,
session: null,
data: {
users: []
}
});
// Creating a fetcher
const fetcher = new BaobabFetcher(tree, [
// Stalking get events on the 'session' path
{
path: ['session'],
// Function to use to trigger fetching
get: client.getSession
},
// Stalking get events on the 'data users' path
{
path: ['data', 'users'],
// We might want to use different functions, in order until one returns valid data
get: [store.getUsers, client.getUsers],
// If we expect specific data and don't want to consider falsey value as invalid
expect: function(data) {
return data.length > 1;
},
// A optional condition to be passed to trigger the fecthing
condition: isUserLogged
}
]);
// Possibility to add hooks to edit the state when fetching and/or errors etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment