Skip to content

Instantly share code, notes, and snippets.

@jmcmaster
Last active April 9, 2018 22:07
Show Gist options
  • Select an option

  • Save jmcmaster/112198bbdce2d4638624caa5ee3b7ed0 to your computer and use it in GitHub Desktop.

Select an option

Save jmcmaster/112198bbdce2d4638624caa5ee3b7ed0 to your computer and use it in GitHub Desktop.
(function() {
var people = {
people: ['Will', 'Matt'],
template: $('#people-template').html(),
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.on('click', 'i.del', this.deletePerson.bind(this));
},
render: function() {
var data = {
people: this.people,
};
this.$ul.html(Mustache.render(this.template, data));
},
addPerson: function(value) {
if (this.$input.val()) {
this.people.push(value || this.$input.val());
this.render();
this.$input.val('');
}
},
deletePerson: function(e) {
var $remove = $(e.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