This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackbone-views-today-list-view.js
56 lines (53 loc) · 1.75 KB
/
backbone-views-today-list-view.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
var TodayListView = Backbone.View.extend({
template: _.template($('#todayList').html()),
initialize: function(options) {
this.properties = options.properties;
this.listenTo(this.collection, 'sync change', this.render);
},
events: {
submit: 'submitCollectionForm',
updateSort: 'updateSort',
changeSite: 'changeSite',
copy: 'copy'
},
render: function() {
this.properties.alert = this.collection.alert;
if (this.properties.alert.type) {
this.properties.alert.class = this.properties.alert.type === 'error' ? 'alert-danger' : 'alert-success';
}
this.$el.html(this.template(this.properties));
var $list = this.$el.find('ul');
this.items = this.collection.map(function(model) {
var item = new TodayItemView({model: model});
$list.append(item.render().$el);
return item;
}, this);
var self = this;
$list.sortable({
stop: function(e, ui) {
self.$el.trigger('updateSort');
}
});
this.$el.find('.close').on('click', function(e) {
self.collection.alert.type = null;
self.properties.alert.type = null;
$(this).parents('.msg-center').hide();
});
return this;
},
submitCollectionForm: function(e) {
e.preventDefault();
this.collection.save();
},
updateSort: function(e) {
this.items.forEach(function(item) {
item.model.set('placementId', item.$el.index() + 1);
});
this.collection.sort();
this.render();
},
changeSite: function(e, id) {
this.collection.setSiteId(id);
this.collection.fetch();
}
});