-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.js
65 lines (59 loc) · 1.51 KB
/
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
57
58
59
60
61
62
63
64
65
window.Member = Backbone.Model.extend({
});
window.MemberList = Backbone.Collection.extend({
model: Member,
url: '/leden'
});
window.Members = new MemberList();
window.MemberView = Backbone.View.extend({
className: 'member',
model: Member,
tagName: 'li',
render: function() {
this.$el.text(this.model.get('name'));
return this;
}
});
window.AddMemberDialog = Backbone.View.extend({
events: {
'keypress #new-member-name': 'saveOnEnter'
},
render: function() {
this.$el.html('<div class="add-member-dialog"><input type="text" id="new-member-name"></div>');
return this;
},
saveOnEnter: function(event) {
if (event.keyCode === 13) {
Members.create({name: $('#new-member-name').val()});
this.$el.remove();
}
}
});
$(function() {
window.AppView = Backbone.View.extend({
el: $('body'),
events: {
'click #new_leden': 'addMemberDialog'
},
initialize: function() {
Members.bind('add', this.addMember, this);
Members.bind('reset', this.addMembers, this);
},
addMember: function(member) {
var view = new MemberView({model: member});
$('#members').append(view.render().el);
},
addMembers: function() {
Members.each(this.addMember);
},
addMemberDialog: function(event) {
console.log('clicked add member');
event.preventDefault();
var view = new AddMemberDialog();
$('body').append(view.render().el);
}
});
window.App = new AppView;
console.log(App.el);
Members.fetch();
});