Last active
May 23, 2016 07:32
-
-
Save jhades/e1ec638492237b5efff25bd3825d05d3 to your computer and use it in GitHub Desktop.
Angular 2 Application Architecture - Building Flux Apps with Redux and Immutable.js
This file contains hidden or 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
{ | |
todos:[ | |
{ | |
"id":1, | |
"description":"TODO 1", | |
"completed":false | |
}, | |
{ | |
"id":2, | |
"description":"TODO 2", | |
"completed":false | |
} | |
], | |
uiState: { | |
actionOngoing: false, | |
message: "Ready" | |
} |
This file contains hidden or 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
function addTodo(newTodo: Todo) { | |
return { | |
type: ADD_TODO, | |
newTodo | |
} | |
} | |
function toggleTodo(todo: Todo) { | |
return { | |
type: TOGGLE_TODO, | |
todo | |
} | |
} | |
This file contains hidden or 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
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; | |
} | |
} | |
This file contains hidden or 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
function uiState(state: List, action) { | |
switch(action.type) { | |
case BACKEND_ACTION_STARTED: | |
return { | |
actionOngoing:true, | |
message: action.message | |
}; | |
case BACKEND_ACTION_FINISHED: | |
... | |
} | |
} | |
This file contains hidden or 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 todoApp = combineReducers({ | |
uiState, | |
todos | |
}); | |
This file contains hidden or 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 {createStore, applyMiddleware} from 'redux'; | |
const createStoreWithMiddleware = | |
applyMiddleware(createLogger())(createStore); | |
const store = createStoreWithMiddleware( | |
todoApp, | |
{ | |
todos:List([]), | |
uiState: initialUiState | |
}); | |
This file contains hidden or 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 {ReduxStore} from "angular2-redux-store"; | |
@Injectable() | |
class TodoStore extends ReduxStore { | |
constructor() { | |
super(store); | |
} | |
} | |
This file contains hidden or 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
class TodoList { | |
constructor(private store: TodoStore) { | |
} | |
toggleTodo(todo) { | |
this.store.dispatch(toggleTodo(todo)); | |
} | |
} | |
This file contains hidden or 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
let todo1 = { | |
id:1, | |
description: "TODO 1", | |
completed: false | |
}; | |
let list = Immutable.List([todo1]); | |
This file contains hidden or 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
let todo2 = { | |
id:2, | |
description: "TODO 2", | |
completed: true | |
}; | |
let list2 = list.push(todo2); // this list now has two elements | |
This file contains hidden or 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
let todo = Immutable.Map({ | |
id:1, | |
description: "TODO 1", | |
completed:false | |
}); |
This file contains hidden or 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
let TodoRecord = Immutable.Record({ | |
id: 0, | |
description: "", | |
completed: false | |
}); | |
This file contains hidden or 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 TodoRecord = Record({ | |
id: 0, | |
description: "", | |
completed: false | |
}); | |
class Todo extends TodoRecord { | |
constructor(props) { | |
super(props); | |
} | |
} | |
This file contains hidden or 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
class Todo extends TodoRecord { | |
id:number; | |
description:string; | |
completed: boolean; | |
constructor(props) { | |
super(props); | |
} | |
} | |
This file contains hidden or 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
class Todo extends TodoRecord { | |
id:number; | |
description:string; | |
completed: boolean; | |
constructor(props) { | |
super(props); | |
} | |
} | |
This file contains hidden or 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
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