Skip to content

Instantly share code, notes, and snippets.

@tomastrajan
Created May 1, 2017 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomastrajan/904f2840640b6049de14b9be78d8f13f to your computer and use it in GitHub Desktop.
Save tomastrajan/904f2840640b6049de14b9be78d8f13f to your computer and use it in GitHub Desktop.
Angular Model Pattern - model mutation
@Injectable()
export class TodosService {
/* ... */
// pass all neccessary data as funcion parameters
toggleTodo(name: string) {
// retrieve model internal state
const todos = this.model.get();
// mutate model internal state
todos.forEach(t => {
if (t.name === name) {
t.done = !t.done;
}
});
// set new model state; and notify all subscribed componenets
this.model.set(todos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment