-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathlist.js
More file actions
48 lines (43 loc) · 1.17 KB
/
list.js
File metadata and controls
48 lines (43 loc) · 1.17 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
(function () {
'use strict';
var $ = window.jQuery;
var _ = window._;
var app = window.app;
app.ListView = Backbone.View.extend({
initialize: function () {
this.views = {};
this.listenTo(this.collection, {
add: this.addModel,
sort: this.sortModels,
remove: this.removeModel
});
this.collection.each(this.addModel, this);
},
addModel: function (model) {
this.$el.append((this.views[model.cid] = new this.options.modelView({
collection: this.collection,
model: model
})).render().el);
},
sortModels: function () {
var views = this.views;
var $el = this.$el;
var $models = $el.children();
this.collection.each(function (model, i) {
var view = views[model.cid];
if (!view) return;
var el = view.el;
if (!$models[i]) {
$el.append(el);
} else if ($models[i] !== el) {
$models.eq(i).before(el);
$models = $($models.get().splice(i, 0, el));
}
});
},
removeModel: function (model) {
this.views[model.cid].remove();
delete this.views[model.cid];
}
});
})();