Last active
October 26, 2015 16:24
-
-
Save Yomguithereal/b77e455766fd489fa54d to your computer and use it in GitHub Desktop.
Baobab fetcher
This file contains 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 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