Last active
September 21, 2015 15:25
-
-
Save clalimarmo/c46fcd5ca37f55424dc1 to your computer and use it in GitHub Desktop.
Deep Linking with React + Flux, Task Store, modified to route on "navigate" Actions
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
TaskStore = (function() { | |
var TaskStore = {}; | |
var state = {}; | |
TaskStore.addChangeListener = function() { | |
// left as an exercise - hint: event emitters | |
}; | |
TaskStore.addChangeListener = function() { | |
// left as an exercise - hint: event emitters | |
}; | |
TaskStore.selectedTask = function() { | |
return state.selectedTask; | |
}; | |
TaskStore.tasks = function() { | |
return Object.keys(state.tasks).map(function(taskId) { | |
return state.tasks[taskId]; | |
}); | |
}; | |
init(); | |
return TaskStore; | |
function init() { | |
var router = Router(); | |
router.register(/^\/tasks\/?$/, function() { | |
state.selectedTask = null; | |
triggerChangeEvent(); | |
}); | |
router.register(/^\/tasks\/(\d+)\/?$/, function(path, matcher) { | |
var taskId = path.match(matcher)[1]; | |
state.selectedTask = state.tasks[taskId]; | |
}); | |
Dispatcher.register(function(action) { | |
switch (action.type) { | |
case 'initialize app': | |
loadTasks(); | |
router.route(action.path); | |
break; | |
// update the store appropriately when the user follows an AppLink | |
case 'navigate': | |
router.route(action.path); | |
break; | |
} | |
}); | |
} | |
function triggerChangeEvent() { | |
// left as an exercise - this should trigger the callbacks added | |
// via the addChangeListener method | |
} | |
function loadTasks() { | |
state.tasks = []; | |
// left as an exercise: add mock task data, or integrate with API | |
triggerChangeEvent(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment