Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Created April 15, 2018 06:09
Show Gist options
  • Select an option

  • Save dmjcomdem/63472be5ffa6a4e81ee499dac30a6f39 to your computer and use it in GitHub Desktop.

Select an option

Save dmjcomdem/63472be5ffa6a4e81ee499dac30a6f39 to your computer and use it in GitHub Desktop.
simple class EventEmitter
class EventEmitter {
constructor() {
this.events = {};
}
on(type, listener) {
this.events[type] = this.events[type] || [];
this.events[type].push(listener);
}
emit(type, arg) {
if (this.events[type]) {
this.events[type].forEach(listener => listener(arg));
}
}
}
class Model extends EventEmitter {
constructor(items = []) {
super();
this.items = items;
}
addItem(item) {
this.items.push(item);
this.emit('add', this.items);
return item;
}
}
class Controller {
constructor(model, view) {
this.model = model;
this.view = view;
view.on('add', this.addTodo.bind(this));
}
addTodo(title) {
const item = this.model.addItem({
id: Date.now(),
title,
completed: false
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment