Skip to content

Instantly share code, notes, and snippets.

@idream3
Last active August 29, 2015 14:21
Show Gist options
  • Save idream3/e426349fd71ebb55f166 to your computer and use it in GitHub Desktop.
Save idream3/e426349fd71ebb55f166 to your computer and use it in GitHub Desktop.
/*
State Exploration 2.
Playing with diferent ways to configure Maps.
*/
var state = {
// application data, changed from `model`.
data: { },
// Maps are related to views so It makes some sense to put them
// under a view domain.
view: {
currentUserId: null,
// 1. array with function as the last parameter
selectedUser: [
'data.users',
'view.selectedUserId',
(Cerebral, state) => {
return state.users[state.selectedUserId]
}],
// 2. function that returns the above
selectedUser: () => {
return ['data.users', 'view.selectedUserId',
(Cerebral, state) => {
return state.users[state.selectedUserId]
}]
},
// 3. object with path and map keys
selectedUser: {
path: ['data.users', 'view.selectedUserId'],
map: (Cerebral, state) => {
return state.users[state.selectedUserId]
}
}
setup: {
locations: {
selectedLocationId: null,
selectedLocation: [
':selectedLocationId', // because of the nesting, the `:` provides a way to point to the current path.
'data.locations.locations',
(Cerebral, state) => {
let location = state.locations[state.selectedLocationId].toJS(); // so we can mutate
let schedules = Cerebral.get('data.locations.schedules');
// grab echedules and attach to location object
location.schedules = location.scheduleIds.filter(id => {
return schedules[id]
})
return location
}],
sortType: 'ASC',
sortBy: 'status',
sortedLocations: [
':sortType',
':sortBy',
'data.locations.locations',
(Cerebral, state) => {
return Object.keys(state.locations).map(id => {
return state.locations[id]
}).sort(/* sort function */)
}]
},
concepts: { ... }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment