Skip to content

Instantly share code, notes, and snippets.

@tylerzey
Created April 4, 2018 14:11
Show Gist options
  • Save tylerzey/a43dbce61c8af0714e2c9b798a5bd971 to your computer and use it in GitHub Desktop.
Save tylerzey/a43dbce61c8af0714e2c9b798a5bd971 to your computer and use it in GitHub Desktop.
found here: https://www.youtube.com/watch?v=m-NYyst_tiY
var people = {
people: ['Will', 'Steve'],
init: function() {
this.cacheDom();
this.bindEvents();
this.render();
},
cacheDom: function() {
this.$el = $('#peopleModule');
this.$button = this.$el.find('button');
this.$input = this.$el.find('input');
this.$ul = this.$el.find('ul');
this.template = this.$el.find('#people-template').html();
},
bindEvents: function() {
this.$button.on('click', this.addPerson.bind(this));
this.$ul.delegate('i.del', 'click', this.deletePerson.bind(this));
},
render: function() {
var data = {
people: this.people,
};
this.$ul.html(Mustache.render(this.template, data));
},
addPerson: function() {
this.people.push(this.$input.val());
this.render();
this.$input.val('');
},
deletePerson: function(event) {
var $remove = $(event.target).closest('li');
var i = this.$ul.find('li').index($remove);
this.people.splice(i, 1);
this.render();
}
};
people.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment