Skip to content

Instantly share code, notes, and snippets.

@jmcmaster
Created April 9, 2018 22:06
Show Gist options
  • Save jmcmaster/d7812d94381bea6b8687f090c96d1b0d to your computer and use it in GitHub Desktop.
Save jmcmaster/d7812d94381bea6b8687f090c96d1b0d to your computer and use it in GitHub Desktop.
var people = (function() {
var people = ['Will', 'Matt'];
// Cache Dom
$el = $('#peopleModule');
$button = this.$el.find('button');
$input = this.$el.find('input');
$ul = this.$el.find('ul');
template = this.$el.find('#people-template').html();
// Bind Events
$button.on('click', addPerson);
$ul.on('click', 'i.del', deletePerson);
// Initial Render
_render();
function _render() {
$ul.html(Mustache.render(template, {people: people}));
}
function addPerson(value) {
var name = (typeof value === "string") ? value : $input.val();
if (!name) return;
people.push(name);
_render();
$input.val('');
}
function deletePerson(event) {
var i;
if (typeof event === "number") {
i = event;
} else if (event.type === 'click') {
var $remove = $(event.target).closest('li');
i = $ul.find('li').index($remove);
} else {
return;
}
people.splice(i, 1);
_render();
}
return {
addPerson: addPerson,
deletePerson: deletePerson
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment