Created
September 5, 2016 13:47
-
-
Save MelleB/654d8763892eb10ea4718691d611b72a to your computer and use it in GitHub Desktop.
Hierarchical models with Choo
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
// Note: Child model is NOT registered with choo.model() | |
const model = { | |
state: { | |
name: '', | |
x: 0, | |
y: 0, | |
width: 0, | |
height: 0, | |
}, | |
reducers: { | |
resizeNode: (data, state) => { | |
// ... | |
return { ...state, width: w, height: h }; | |
} | |
} | |
} | |
export default (name, x, y) => { return { ...model, state: { ... model.state, name, x, y } } } |
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
import newTask from 'models/task' | |
export default { | |
namespace: 'project', | |
state: { | |
tasks: [ | |
newTask('task', 100, 100), | |
newTask('task', 250, 100), | |
]}, | |
reducers: { | |
addTask: (data, state) => { | |
///... | |
state.tasks.push(newTask(name, x, y)) | |
return state; | |
}, | |
reduceAtTask: (data, state) => { | |
let task = state.tasks[data.index]; | |
let reducer = task.reducers[data.reducer]; | |
let index = data.index; | |
delete data.reducer; | |
delete data.index; | |
let newState = reducer(data, task.state); | |
let tasks = state.tasks.slice(); | |
tasks[index].state = newState; | |
return { ...state, tasks }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wathes*