Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active May 23, 2016 07:32
Show Gist options
  • Save jhades/e1ec638492237b5efff25bd3825d05d3 to your computer and use it in GitHub Desktop.
Save jhades/e1ec638492237b5efff25bd3825d05d3 to your computer and use it in GitHub Desktop.
Angular 2 Application Architecture - Building Flux Apps with Redux and Immutable.js
{
todos:[
{
"id":1,
"description":"TODO 1",
"completed":false
},
{
"id":2,
"description":"TODO 2",
"completed":false
}
],
uiState: {
actionOngoing: false,
message: "Ready"
}
function addTodo(newTodo: Todo) {
return {
type: ADD_TODO,
newTodo
}
}
function toggleTodo(todo: Todo) {
return {
type: TOGGLE_TODO,
todo
}
}
function todos(state: List, action) {
switch(action.type) {
case ADD_TODO:
return state.push(action.newTodo);
case TOGGLE_TODO:
return toggleTodo(state, action);
...
default:
return state;
}
}
function uiState(state: List, action) {
switch(action.type) {
case BACKEND_ACTION_STARTED:
return {
actionOngoing:true,
message: action.message
};
case BACKEND_ACTION_FINISHED:
...
}
}
const todoApp = combineReducers({
uiState,
todos
});
import {createStore, applyMiddleware} from 'redux';
const createStoreWithMiddleware =
applyMiddleware(createLogger())(createStore);
const store = createStoreWithMiddleware(
todoApp,
{
todos:List([]),
uiState: initialUiState
});
import {ReduxStore} from "angular2-redux-store";
@Injectable()
class TodoStore extends ReduxStore {
constructor() {
super(store);
}
}
class TodoList {
constructor(private store: TodoStore) {
}
toggleTodo(todo) {
this.store.dispatch(toggleTodo(todo));
}
}
let todo1 = {
id:1,
description: "TODO 1",
completed: false
};
let list = Immutable.List([todo1]);
let todo2 = {
id:2,
description: "TODO 2",
completed: true
};
let list2 = list.push(todo2); // this list now has two elements
let todo = Immutable.Map({
id:1,
description: "TODO 1",
completed:false
});
let TodoRecord = Immutable.Record({
id: 0,
description: "",
completed: false
});
const TodoRecord = Record({
id: 0,
description: "",
completed: false
});
class Todo extends TodoRecord {
constructor(props) {
super(props);
}
}
class Todo extends TodoRecord {
id:number;
description:string;
completed: boolean;
constructor(props) {
super(props);
}
}
class Todo extends TodoRecord {
id:number;
description:string;
completed: boolean;
constructor(props) {
super(props);
}
}
let todo:Todo = new Todo({id: 1, description: "I'm Type Safe!"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment