-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
162 lines (138 loc) · 4.3 KB
/
index.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
156
157
158
159
160
161
162
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Darts</title>
<!-- ========= -->
<!-- CSS -->
<!-- ========= -->
<style type="text/css">
#dartapp ul {
list-style-type: none; /* Hides bullet points from dart list */
}
</style>
</head>
<body>
<!-- ========= -->
<!-- Your HTML -->
<!-- ========= -->
<section id="dartapp">
<header id="header">
<h1>Darts</h1>
<li>1.figure out how to sort $el</li>
<li>2.make name field and score field</li>
<li>3.sort by score</li>
<li>4.style a little bit</li>
<li>5.write method for total score</li>
<li>6.rank by total score(scores associated with a name SUM'ed)</li>
<li>7.make app presentable</li>
<input id="new-dart" placeholder="name " autofocus>
</header>
<section id="main">
<ul id="dart-list"></ul>
</section>
</section>
<!-- Templates -->
<script type="text/template" id="item-template">
<div class="view">
<label><%- score %></label>
<button class="destroy">remove</button>
</div>
</script>
<!-- ========= -->
<!-- Libraries -->
<!-- ========= -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js" type="text/javascript"></script>
<!-- =============== -->
<!-- Javascript code -->
<!-- =============== -->
<script type="text/javascript">
'use strict';
var app = {}; // create namespace for our app
//--------------
// Models
//--------------
app.Dart = Backbone.Model.extend({
defaults: {
score: '',
name: ''
}
});
//--------------
// Collections
//--------------
app.DartList = Backbone.Collection.extend({
model: app.Dart,
comparator: function(item) {
return item.get('score');
},
localStorage: new Store("backbone-darts")
});
// instance of the Collection
app.dartList = new app.DartList();
//--------------
// Views
//--------------
// renders individual dart items list (li)
app.DartView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#item-template').html()),
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this; // enable chained calls
},
initialize: function(){
this.model.on('destroy', this.remove, this); // remove: Convenience Backbone's function for removing the view from the DOM.
},
events: {
'click .destroy': 'destroy'
},
destroy: function(){
this.model.destroy();
}
});
// renders the full list of dart items calling DartView for each one.
app.AppView = Backbone.View.extend({
el: '#dartapp',
initialize: function () {
this.input = this.$('#new-dart');
app.dartList.on('add', this.addAll, this);
app.dartList.on('reset', this.addAll, this);
app.dartList.fetch(); // Loads list from local storage
},
events: {
'keypress #new-dart': 'createDartOnEnter'
},
createDartOnEnter: function(e){
if ( e.which !== 13 || !this.input.val().trim() ) { // ENTER_KEY = 13
return;
}
app.dartList.create(this.newAttributes());
this.input.val(''); // clean input box
},
addOne: function(dart){
var view = new app.DartView({model: dart});
$('#dart-list').append(view.render().el);
},
addAll: function(){
this.$('#dart-list').html(''); // clean the dart list
app.dartList.each(this.addOne, this);
},
newAttributes: function(){
return {
score: this.input.val().trim(),
name: "test name"
}
}
});
//--------------
// Initializers
//--------------
app.appView = new app.AppView();
</script>
</body>
</html>