-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquizitems.coffee
186 lines (151 loc) · 5.94 KB
/
quizitems.coffee
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Load the application once the DOM is ready, using a `jQuery.ready` shortcut.
$ ->
### QuizItem Model ###
# Our basic **QuizItem** model has `answer`, `order`, and `done` attributes.
class QuizItem extends Backbone.Model
# Default attributes for the quizitem.
defaults:
question: "Unpopulated question"
answer: "(Unanswered)"
correctanswer: ""
# Ensure that each quizitem created has `answer`.
initialize: ->
if !@get("answer")
@set({ "answer": @defaults.answer })
# Remove this QuizItem from *localStorage* and delete its view.
clear: ->
@destroy()
@view.remove()
### QuizItem Collection ###
# The collection of quizitems is backed by *localStorage* instead of a remote
# server.
class QuizItemList extends Backbone.Collection
# Reference to this collection's model.
model: QuizItem
# Save all of the quizitem items under the `"quizitems"` namespace.
localStorage: new Store("flexiontest")
# We keep the QuizItems in sequential order, despite being saved by unordered
# GUID in the database. This generates the next order number for new items.
nextOrder: ->
return 1 if !@length
return @last().get('order') + 1
# QuizItems are sorted by their original insertion order.
comparator: (quizitem) ->
return quizitem.get("order")
### QuizItem Item View ###
# The DOM element for a quizitem item...
class QuizItemView extends Backbone.View
#... is a list tag.
tagName: "li"
# Cache the template function for a single item.
template: _.template($("#item-template").html())
# The DOM events specific to an item.
events:
"click div.quizitem-answer": "edit",
"keypress .quizitem-input": "updateOnEnter"
# The QuizItemView listens for changes to its model, re-rendering. Since there's
# a one-to-one correspondence between a **QuizItem** and a **QuizItemView** in this
# app, we set a direct reference on the model for convenience.
initialize: ->
@model.bind('change', this.render)
@model.view = this
# Re-render the answers of the quizitem item.
render: =>
this.$(@el).html(@template(@model.toJSON()))
@setAnswer()
return this
setAnswer: ->
answer = @model.get("answer")
this.$(".quizitem-answer").text(answer)
@input = this.$(".quizitem-input")
if answer != @model.defaults.answer
this.$(".quizitem-input[value=" + answer + ']').attr('checked', 'checked');
# @input.bind("blur", @close)
# @input.val(answer)
# Switch this view into `"editing"` mode, displaying the input field.
edit: =>
this.$(@el).addClass("editing")
@input.focus()
@input.bind("change", @close)
return
# Close the `"editing"` mode, saving changes to the quizitem.
close: =>
selectedAnswer = this.$(":checked").val()
@model.save({ answer: selectedAnswer })
$(@el).removeClass("editing")
correctAnswerDisplay = $(@el).find('.quizitem-correctanswer')
if selectedAnswer == @model.get("correctanswer")
correctAnswerDisplay.addClass('isCorrect')
else
correctAnswerDisplay.addClass('isWrong')
correctAnswerDisplay.fadeIn();
return
# If you hit `enter`, we're through editing the item.
updateOnEnter: (e) =>
@close() if e.keyCode is 13
# Remove this view from the DOM.
remove: ->
$(@el).remove()
# Remove the item, destroy the model.
clear: () ->
@model.clear()
### The Application ###
# Our overall **AppView** is the top-level piece of UI.
class AppView extends Backbone.View
# Instead of generating a new element, bind to the existing skeleton of
# the App already present in the HTML.
el_tag = "#quizitemapp"
el: $(el_tag)
# Delegated events for creating new items, and clearing completed ones.
# events:
# "keypress #new-quizitem": "createOnEnter",
# "click .quizitem-clear a": "clearCompleted"
# At initialization we bind to the relevant events on the `QuizItems`
# collection, when items are added or changed. Kick things off by
# loading any preexisting quizitems that might be saved in *localStorage*.
initialize: =>
@input = this.$("#new-quizitem")
QuizItems.bind("add", @addOne)
QuizItems.bind("reset", @addAll)
QuizItems.bind("all", @render)
QuizItems.fetch()
render: =>
this.$('#quizitem-stats').html()
# Add a single quizitem item to the list by creating a view for it, and
# appending its element to the `<ul>`.
addOne: (quizitem) =>
view = new QuizItemView({model: quizitem})
this.$("#quizitem-list").append(view.render().el)
# Add all items in the **QuizItems** collection at once.
addAll: =>
QuizItems.each(@addOne)
;
# Generate the attributes for a new QuizItem item.
newAttributes: ->
return {
answer: @input.val(),
order: QuizItems.nextOrder(),
}
# If you hit return in the main input field, create new **QuizItem** model,
# persisting it to *localStorage*.
createOnEnter: (e) ->
return if (e.keyCode != 13)
QuizItems.create(@newAttributes())
@input.val('')
# Clear all done quizitem items, destroying their models.
clearCompleted: ->
_.each(QuizItems.done(), (quizitem) ->
quizitem.clear()
)
return false
# Create our global collection of **QuizItems**.
QuizItems = new QuizItemList() #It is supposed to be possible to pass in a collection here but couldn't get it to work
App = new AppView()
if QuizItems.length is 0
$.each([
{ "question": "Tim Berners-Lee invented the Internet.", "correctanswer": "true"},
{ "question": "Dogs are better than cats.", "correctanswer": "false"},
{ "question": "Winter is coming.", "correctanswer": "true"},
{ "question": "Internet Explorer is the most advanced browser on Earth.", "correctanswer": "false"}
], (i, q) ->
QuizItems.create(q))