-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
85 lines (67 loc) · 2.16 KB
/
script2.js
File metadata and controls
85 lines (67 loc) · 2.16 KB
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
var app = app || {};
app.Sentence = Backbone.Model.extend({
// what position commas are in
defaults: {
content: '',
//content Array after split
contentArray: [],
commas: []
},
initialize: function() {
// splits on space in initialize
this.set('contentArray', this.get('content').split(' '));
}
});
var Sentences = Backbone.Collection.extend({
model: app.Sentence,
// keeps track of which sentence we are on
sentenceNumber: 0
});
app.AppView = Backbone.View.extend({
initialize: function() {
this.render();
this.checkCommas();
},
el: '.container',
template: _.template($('#sentence-template').html()),
events: {
'click .space': 'toggleComma',
'click #next': 'nextSentence'
},
checkCommas: function() {
// iterates through and checks where commas are and updates model to have indexes of where commas are
var commasArray = [];
$('.comma').each( function() {
commasArray.push( $('.space').index($(this)));
});
this.collection.at(this.collection.sentenceNumber).set('commas', commasArray)
},
nextSentence: function() {
// gets next sentence
this.checkCommas();
this.collection.sentenceNumber++;
this.render();
},
toggleComma: function(e) {
// toggleComma based on if comma exists
var sel = $(e.currentTarget);
if (sel.hasClass('comma')) {
sel.html(' ');
sel.removeClass('comma');
} else {
sel.html(', ');
sel.addClass('comma');
}
},
render: function() {
if (this.collection.sentenceNumber < this.collection.length)
this.$el.html(this.template({wordArray: this.collection.at(this.collection.sentenceNumber).get('contentArray')}));
else
this.$el.html('End of sentences.');
}
})
$('#start').click(function() {
// would be a fetch if coming from database
app.sentences = new Sentences([{ content: 'The Caltrain, broke down so I was late to work.'},{content: 'I love to eat, bread, cheese, and eggs every morning.'}]);
new app.AppView({collection: app.sentences});
});