MVC 정리 210720 - ChoDragon9/posts GitHub Wiki

1

  • Model-View: Observer Pattern
  • View-Controller: Strategy Pattern
  • View-Views: Composite Pattern
const model = new Model();
const view = new View(model);
const controller = new Controller(model, view);

view.setController(controller);

document.body.appendChild(view.build());

2

class View {
  build () {
    const div = document.createElement('div');
    const items = this.model.getState();
    
    items.forEach((item) => {
      const childView = new ChildView(this.model);
      childView.setController(this.controller);
      div.appendChild(childView.build());
    });

    return div;
  }
}

3

const modelCollection = new ModelCollection();
const model1 = new Model();
const model2 = new Model();

modelCollection.add(model1);
modelCollection.add(model2);

const view = new View(modelCollection);
class View {
  build () {
    const div = document.createElement('div');
    const models = this.model.getModels();
    
    models.forEach((model) => {
      const childView = new ChildView(model);
      const childController = new ChildController(model, childView);

      childView.setController(childController);

      div.appendChild(childView.build());
    });

    return div;
  }
}