**데이터베이스, 상수, 변수**
를 의미**박스 위치 정보, 글자 내용, 글자 위치, 글자 포맷**
등에 대한 정보를 모두 가지고 있어야한다.// Model
class TodoListModel {
constructor() {
this.todos = [];
}
addTodo(todoText) {
this.todos.push({ id: Date.now(), text: todoText, completed: false });
}
toggleTodoCompletion(todoId) {
this.todos = this.todos.map(todo =>
todo.id === todoId ? { ...todo, completed: !todo.completed } : todo
);
}
deleteTodo(todoId) {
this.todos = this.todos.filter(todo => todo.id !== todoId);
}
}
// View
class TodoListView {
constructor() {
this.todoList = document.getElementById('todo-list');
}
render(todos) {
this.todoList.innerHTML = '';
todos.forEach(todo => {
const todoItem = document.createElement('li');
todoItem.textContent = todo.text;
todoItem.style.textDecoration = todo.completed ? 'line-through' : 'none';
todoItem.addEventListener('click', () => controller.toggleTodoCompletion(todo.id));
this.todoList.appendChild(todoItem);
});
}
}
// Controller
class TodoListController {
constructor(model, view) {
this.model = model;
this.view = view;
}
addTodo(todoText) {
this.model.addTodo(todoText);
this.view.render(this.model.todos);
}
toggleTodoCompletion(todoId) {
this.model.toggleTodoCompletion(todoId);
this.view.render(this.model.todos);
}
deleteTodo(todoId) {
this.model.deleteTodo(todoId);
this.view.render(this.model.todos);
}
}
// Initialize
const model = new TodoListModel();
const view = new TodoListView();
const controller = new TodoListController(model, view);
view.render(model.todos); // Initial render
// Example usage
controller.addTodo('Buy groceries');
controller.addTodo('Do laundry');
controller.toggleTodoCompletion(model.todos[0].id);
controller.deleteTodo(model.todos[1].id);
저자님께서 토론을 통해 정정하심…(링크)
초기 페이스북 PHP → MVC 패턴으로 인한 문제 발생 → FLUX 패턴을 적용한 React가 탄생한 계기