Commit 60c55a87 authored by Colin Eberhardt's avatar Colin Eberhardt Committed by Sam Saccone

Using two-way binding

This removes the need to reference a DOM element from the component
parent b2d4923a
<section class="todoapp"> <section class="todoapp">
<header class="header"> <header class="header">
<h1>todos</h1> <h1>todos</h1>
<input class="new-todo" placeholder="What needs to be done?" autofocus="" #newtodo (keyup)="addTodo($event, newtodo)"> <input class="new-todo" placeholder="What needs to be done?" autofocus="" [(ngModel)]="newTodoText" (keyup.enter)="addTodo()">
</header> </header>
<section class="main" *ngIf="todoStore.todos.length > 0"> <section class="main" *ngIf="todoStore.todos.length > 0">
<input class="toggle-all" type="checkbox" *ngIf="todoStore.todos.length" #toggleall [checked]="todoStore.allCompleted()" (click)="todoStore.setAllTo(toggleall.checked)"> <input class="toggle-all" type="checkbox" *ngIf="todoStore.todos.length" #toggleall [checked]="todoStore.allCompleted()" (click)="todoStore.setAllTo(toggleall.checked)">
......
...@@ -9,9 +9,9 @@ var __metadata = (this && this.__metadata) || function (k, v) { ...@@ -9,9 +9,9 @@ var __metadata = (this && this.__metadata) || function (k, v) {
}; };
var core_1 = require('angular2/core'); var core_1 = require('angular2/core');
var store_1 = require('./services/store'); var store_1 = require('./services/store');
var ENTER_KEY = 13;
var TodoApp = (function () { var TodoApp = (function () {
function TodoApp() { function TodoApp() {
this.newTodoText = '';
this.todoStore = new store_1.TodoStore(); this.todoStore = new store_1.TodoStore();
} }
TodoApp.prototype.stopEditing = function (todo, editedTitle) { TodoApp.prototype.stopEditing = function (todo, editedTitle) {
...@@ -41,10 +41,10 @@ var TodoApp = (function () { ...@@ -41,10 +41,10 @@ var TodoApp = (function () {
TodoApp.prototype.remove = function (todo) { TodoApp.prototype.remove = function (todo) {
this.todoStore.remove(todo); this.todoStore.remove(todo);
}; };
TodoApp.prototype.addTodo = function ($event, newtodo) { TodoApp.prototype.addTodo = function () {
if ($event.which === ENTER_KEY && newtodo.value.trim().length) { if (this.newTodoText.trim().length) {
this.todoStore.add(newtodo.value); this.todoStore.add(this.newTodoText);
newtodo.value = ''; this.newTodoText = '';
} }
}; };
TodoApp = __decorate([ TodoApp = __decorate([
......
import {Component} from 'angular2/core'; import {Component} from 'angular2/core';
import {TodoStore, Todo} from './services/store'; import {TodoStore, Todo} from './services/store';
const ENTER_KEY = 13;
@Component({ @Component({
selector: 'todo-app', selector: 'todo-app',
templateUrl: 'app/app.html' templateUrl: 'app/app.html'
}) })
export default class TodoApp { export default class TodoApp {
todoStore: TodoStore; todoStore: TodoStore;
newTodoText = '';
constructor() { constructor() {
this.todoStore = new TodoStore(); this.todoStore = new TodoStore();
...@@ -50,10 +49,10 @@ export default class TodoApp { ...@@ -50,10 +49,10 @@ export default class TodoApp {
this.todoStore.remove(todo); this.todoStore.remove(todo);
} }
addTodo($event: KeyboardEvent, newtodo: HTMLInputElement) { addTodo() {
if ($event.which === ENTER_KEY && newtodo.value.trim().length) { if (this.newTodoText.trim().length) {
this.todoStore.add(newtodo.value); this.todoStore.add(this.newTodoText);
newtodo.value = ''; this.newTodoText = '';
} }
} }
} }
...@@ -42,6 +42,7 @@ var TodoStore = (function () { ...@@ -42,6 +42,7 @@ var TodoStore = (function () {
}; };
TodoStore.prototype.removeCompleted = function () { TodoStore.prototype.removeCompleted = function () {
this.todos = this.getWithCompleted(false); this.todos = this.getWithCompleted(false);
this.updateStore();
}; };
TodoStore.prototype.getRemaining = function () { TodoStore.prototype.getRemaining = function () {
return this.getWithCompleted(false); return this.getWithCompleted(false);
......
...@@ -49,6 +49,7 @@ export class TodoStore { ...@@ -49,6 +49,7 @@ export class TodoStore {
removeCompleted() { removeCompleted() {
this.todos = this.getWithCompleted(false); this.todos = this.getWithCompleted(false);
this.updateStore();
} }
getRemaining() { getRemaining() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment