-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
132 lines (106 loc) · 3.13 KB
/
test.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
$(function() {
Backbone.sync = function(method, model, success, error){
success();
}
var Name = Backbone.Model.extend({
defaults: {
first: 'John',
last: 'Doe'
}
});
var FriendList = Backbone.Collection.extend({
model: Name
});
var NameView = Backbone.View.extend({
tagName: 'li',
events: {
'click span.swap': 'swap',
'click span.delete': 'remove'
},
initialize: function(){
_.bindAll(this, 'render', 'unrender', 'swap', 'remove');
this.model.bind('change', this.render);
this.model.bind('remove', this.unrender);
},
render: function(){
$(this.el).html('<span style="color:black;">' +
this.model.get('first') + ' ' +
this.model.get('last') +
'</span> ' +
'<span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;">[swap]</span> ' +
'<span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;">[delete]</span>');
return this;
},
unrender: function(){
$(this.el).remove();
},
swap: function(){
var swapped = {
first: this.model.get('last'),
last: this.model.get('first')
};
this.model.set(swapped);
},
remove: function(){
this.model.destroy();
}
});
// var FileEmailList = Backbone.Model.extend({
// });
// var FileEmailView = Backbone.View.extend({
// initialize: function(){
// _.bindAll(this, 'render', 'uploadFile');
// this.render();
// },
// render: function(){
// $(this.el).html("<input type='file'/>");
// return this;
// }
// });
var FriendsView = Backbone.View.extend({
el: $('body'),
events: {
'click button#btn': 'addFriend',
'change #fileSelect': 'getFileData'
},
initialize: function(){
_.bindAll(this, 'render', 'addFriend', 'renderNewFriend', 'getFileData');
this.collection = new FriendList();
this.collection.bind('add', this.renderNewFriend);
this.counter = 0;
this.render();
},
render: function(){
$(this.el).append("<button id='btn'>Add friend</button>");
$(this.el).append("<input id='fileSelect' type='file'>");
$(this.el).append("<ul></ul>");
},
getFileData: function(event){
var file = event.target.files[0];
var fileReader = new FileReader();
fileReader.onload = function(e){
var output = e.target.result;
var lines = output.split("\n");
alert(lines[0]);
};
fileReader.readAsText(file);
},
addFriend: function(){
this.counter++;
var name = new Name();
name.set({
first: name.get('first') + this.counter
});
this.collection.add(name);
},
renderNewFriend: function(name){
var nameView = new NameView({
model: name
});
// $('ul').append("<li>" + name.get('first') +
// " " + name.get('last') + "</li>");
$('ul').append(nameView.render().el);
}
});
var view = new FriendsView();
});