-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (86 loc) · 1.68 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Module dependencies.
*/
var Emitter = require('emitter');
/**
* Export `List`.
*/
module.exports = List;
/**
* Initialize a new `List`.
*
* @param {Function} View Constructor for the list's items.
*/
function List (View) {
this.View = View;
this.models = {};
this.elements = {};
this.views = {};
//this.list = dom([]);
this.el = document.createElement('ul');
this.el.className = 'content topcoat-list';
}
/**
* Use a given `plugin`.
*
* @param {Function} plugin
*/
List.use = function (plugin) {
plugin(this);
return this;
};
/**
* Mixin emitter.
*/
Emitter(List.prototype);
/**
* Add an item to the list.
*
* @param {Model} model
*/
List.prototype.add = function (model) {
var id = model.primary(), el, view;
if (this.views[id]) {
//console.log(this.views);
} else {
view = new this.View(model);
this.el.appendChild(view.el);
this.views[id] = view;
}
return this;
};
/**
* Remove an item from the list.
*
* @param {String} id
*/
List.prototype.remove = function (id) {
var view = this.views[id];
if (view) {
this.el.removeChild(view.el);
this.emit('remove', view);
delete this.views[id];
if (!view.el) return this;
}
//this.list = this.list.reject(function (item) { el === item.get(0); });
return this;
};
/**
* Filter the list's elements by hiding ones that don't match.
*
* @param {Function} fn Filtering function.
*/
List.prototype.filter = function (fn) {
//this.list.removeClass('hidden');
//this.list.reject(fn).addClass('hidden');
return this;
};
/**
* Empty the list.
*/
List.prototype.empty = function () {
for (var id in this.views) {
this.remove(id);
}
return this;
};