-
Notifications
You must be signed in to change notification settings - Fork 0
/
underscore2.html
155 lines (126 loc) · 3.07 KB
/
underscore2.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>my first backbone</title>
<script type="text/javascript" src="lib/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="lib/underscore-min.js"></script>
<script type="text/javascript" src="lib/backbone-min.js"></script>
<script
type="text/javascript"
src="lib/backbone.localStorage-min.js"></script>
<script type="text/template" id="teamTemplate">
<ul>
<li><%= name %></li>
<li><%= local %></li>
</ul>
</script>
<script type="text/javascript">
var app = app || {};
app.team = Backbone.Model.extend({
defaults: {
name: "",
local: ""
}
});
app.teams = Backbone.Collection.extend({
localStorage: new Backbone.LocalStorage("TeamsCollection"),
model: app.team
});
app.teamView = Backbone.View.extend({
tagName: "div",
template: _.template($("#teamTemplate").html()),
render: function () {
this.$el.html(this.template(this.model.attributes));
return this;
}
});
app.teamsView = Backbone.View.extend({
el: "#teams",
initialize: function (initialTeams) {
this.collection = new app.teams();
this.collection.fetch();
this.render();
this.listenTo(this.collection, "add", this.renderTeam);
},
events: {
"click #showNew": "toggleForm",
"click #add": "addTeam"
},
toggleForm: function () {
var el = $("#addTeam");
el.toggle();
if (el.is(":visible")) {
$("#showNew").html("hide form");
} else {
$("#showNew").html("show form");
}
},
addTeam: function (e) {
e.preventDefault();
var data = {};
data["name"] = $("#name").val();
data["local"] = $("#local").val();
this.collection.add(new app.team(data));
this.startOver();
},
startOver: function () {
$("#name").val("");
$("#local").val("");
$("#name").focus();
},
render: function () {
this.collection.each(function (item) {
this.renderTeam(item);
}, this);
},
renderTeam: function (item) {
var teamView_ = new app.teamView({
model: item
});
this.$el.append(teamView_.render().el);
}
});
$(document).ready(function () {
new app.teamsView();
$("#name").focus();
});
</script>
<style>
body {
font-family: verdana;
font-size: medium;
}
label.title {
font-size: xx-large;
font-weight: bold;
text-decoration: overline underline;
}
a#showNew {
margin-left: 30px;
}
form > p > label {
width: 80px;
}
form > p > label,
form > input {
float: left;
}
</style>
</head>
<body>
<div id="teams">
<p>
<label class="title">new team</label>
<a href="#" id="showNew">hide form</a>
</p>
<form id="addTeam" action="#">
<p><label>name::</label><input id="name" type="text" /></p>
<p><label>local::</label><input id="local" type="text" /></p>
<p><button id="add">Add</button></p>
</form>
<br />
<label class="title">list of teams</label>
</div>
</body>
</html>