This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollectionView.js
197 lines (149 loc) · 5.33 KB
/
CollectionView.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* OPINIONS
*
* 1) Collection should render automatically
* 2) Sub-views should be rendered once on creation unless manually re-rendered
* 3) CollectionView should not need to be manually re-rendered except for rare cases
*
* Dependencies:
* - CollectionItemView
*
*/
(function() {
var CollectionView = Backbone.View.extend({
tagName: "div",
itemViewConstructor: Backbone.CollectionItemView,
itemViews: null,
itemOptions: null,
sortKey: null,
initialize: function(options) {
this.itemViews = [];
this.itemOptions = {};
this._parseOptions(options);
this._bindEvents();
this.reset();
},
// This function will be called manually by the user
// It will obliterate the current el. When adding sub-views
// back to the DOM, it will be necessary to delegateEvents
// again. Event delegation will be up to the item view.
render: function() {
this.$el.html('');
_.each(this.itemViews, function(view) {
if (this._collectionFilter(view.model)) {
view.delegateEvents(view.events);
this.$el.append(view.$el);
}
}, this);
return this;
},
// This will be bound to the collection's add event
add: function(item, options) {
var view = this._createItemView(item);
options = _.defaults(options || {}, {
index: false
});
if (options && options.index && (options.index < this.itemViews.length)) {
var pivotView = this.itemViews[options.index];
this.itemViews.splice(options.index, 0, view);
if (this._collectionFilter(item)) {
pivotView.$el.before(view.$el);
}
} else {
this.itemViews.push(view);
if (this._collectionFilter(item)) {
this.$el.append(view.$el);
}
}
},
// This will be bound to the collection's remove event
remove: function(item, options) {
var viewIndex = -1;
view = _.find(this.itemViews, function(view, index) {
if (item.cid == view.model.cid) {
viewIndex = index;
return true;
}
});
view.remove();
this.itemViews.splice(viewIndex, 1);
},
// This will be bound to the collection's reset event
// "reset" is posted from the following:
// 1) Collection is reset
// 2) Collection is fetched
// 3) Collection is sorted
//
reset: function(options) {
_.each(this.itemViews, function(view) {
view.remove();
});
this.itemViews = [];
this.collection.each(function(item) {
this.add(item);
}, this);
},
setSortKey: function(sortKey, comparator) {
if (this.sortKey) {
this.collection.off("change:" + this.sortKey);
}
this.sortKey = sortKey;
if (comparator) {
this.collection.comparator = comparator;
} else {
this.collection.comparator = function(item1, item2) {
return item1.get(sortKey) > item2.get(sortKey);
};
}
this.collection.on("change:" + this.sortKey, function() {
this.collection.sort();
}, this);
this.collection.sort();
},
setFilter: function(filter) {
this._collectionFilter = filter;
this.render();
},
_collectionFilter: function(item) {
return true;
},
// It will be possible to override
// many options with configuration
// variables rather than subclasses.
// This allows for faster productivity
_parseOptions: function(options) {
if (options.itemViewConstructor) {
this.itemViewConstructor = options.itemViewConstructor();
}
if (options.itemOptions) {
this.itemOptions = options.itemOptions;
}
if (options.filter) {
this.setFilter(options.filter);
}
},
_createItemView: function(item) {
var view = new this.itemViewConstructor(_.defaults({
model: item
}, this.itemOptions));
view.on("click", function(event, item) {
this.trigger("click", event, item, this.collection);
}, this);
return view;
},
_bindEvents: function() {
this.collection.on("add", function(model, collection, options) {
this.add(model, options);
}, this);
this.collection.on("remove", function(model, collection, options) {
this.remove(model, options);
}, this);
this.collection.on("reset", function(collection, options) {
this.reset(options);
}, this);
}
});
_.extend(Backbone, {
CollectionView: CollectionView
});
})();