-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevealingModularPattern.js
More file actions
57 lines (49 loc) · 1.08 KB
/
Copy pathrevealingModularPattern.js
File metadata and controls
57 lines (49 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
var people = (function(){
var name = 'Default';
function sayName(){
console.log(name);
};
return {
sayName: sayName
};
})();
*/
var people = (function(){
var people = ['John', 'Will'];
// cache DOM
var $el = $('#peopleModule');
var $button = this.$el.find('button');
var $input = this.$el.find('input');
var $ul = this.$el.find('ul');
var template = this.$el.find('#people-template').html();
// bind Events
button.on('click', addPerson);
$ul.delegate('i.del', 'click', deletePerson);
// method is private and not revealed outside
_render();
function _render(){
$ul.html(Mustache.render(template, {people: people}));
};
function addPerson(value){
var name = (typeof value === "string") ? value : $input.val();
people.push(name);
_render();
$input.val('');
};
function deletePerson(event){
var i;
if(typeof event === "number") {
i = event;
} else {
var $remove = $(event.target).closest('li');
i = $ul.find('li').indexOf($remove);
}
people.slice(i, 1);
_render();
};
return {
addPerson: addPerson,
deletePerson: deletePerson
};
})();