-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthday.js
143 lines (132 loc) · 4.58 KB
/
birthday.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
133
134
135
136
137
138
139
140
141
142
143
(function($){
//month model
var Month = Backbone.Model.extend({
defaults : function () {
return {
name : "",
number : 0,
selected : false //trailing comment
}
},
});
//Month view
var MonthView = Backbone.View.extend({
tagName : "div",
class : "month",
events : {
"click" : "select"
},
select : function(e) {
birthdayMonth.setMonth(this.model.get('name'));
this.render();
},
render: function () {
if (this.model.get('name') === birthdayMonth.getMonth()) {
this.$el.html(this.model.get('name')).css({color : "#F13737"});
} else {
this.$el.html(this.model.get('name'));
}
return this;
}
});
//The Year collection is a collection of Months. It has a custom comparator for sorting by number.
var Year = Backbone.Collection.extend({
model : Month,
year : 0,
sortKey : "number",
comparator : function (model) {
return model.get(this.sortKey);
}
});
//YearView which renders a view of views
var YearView = Backbone.View.extend({
template : _.template($('#year-template').html()),
tagName: "months",
events : {
"click" : "clicked"
},
initialize : function () {
_.bindAll(this, 'initRender', 'addAll', 'addOne');
this.initRender();
},
clicked : function () {
var selectedMonth = this.collection.where({"name" : birthdayMonth.getMonth()});
console.log("clicked");
this.initRender();
},
initRender: function() {
this.$el.html(this.template);
this.addAll();
return this;
},
addAll: function() {
this.collection.each(this.addOne);
},
addOne: function(mon) {
var monView = new MonthView({model : mon});
this.$el.append(monView.render().el);
}
});
//birthdayMonth API, the user clicking functionality is linked to the AppView's events instead of having them directly here too
var birthdayMonth = (function (initMon) {
var currentMonth = "";
return {
getMonth : function () {
return this.currentMonth;
},
setMonth : function (name) {
this.currentMonth = name;
console.log("User selected " + name + " as the new month");
}
};
})("December");
//AppView is the main view for the application
var AppView = Backbone.View.extend({
template : _.template($('#button-template').html()),
events : {
"sort" : "sorted"
},
sorted : function () {
console.log("Sorting");
},
//AppView listens to clicks from the buttons which sort the months and clicks to the month div
events : {
"click #numButton" : "byNumber",
"click #nameButton" : "byName",
},
//this sorts the months based on the Collections default comparator (Number) and then re-renders
byNumber : function (e) {
console.log("byNumber");
this.collection.sortKey = "number";
this.collection.sort();
this.render();
},
//this sorts the months with a custom sorting function (by Name) and then renders the view based on the resultant list
byName : function (e) {
console.log("byName");
this.collection.sortKey = "name";
this.collection.sort();
this.render();
},
//render the App's year
render: function () {
this.$el.html(this.template);
var yearView = new YearView({collection : this.collection});
this.$el.append(yearView.initRender().el);
return this;
}
});
var mon = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var months = new Year();
var years = [];
var yearViews = [];
var baseElement = $("#app");
for (var i = 0; i < 12; i += 1) {
months.add( new Month({ "name" : mon[i], "number" : (i+1)}));
}
for (var i = 0; i < 1000; i += 1) {
years[i] = months.clone();
yearViews[i] = new AppView({collection : years[i], id : (2014 - i)});
baseElement.append(yearViews[i].render().el);
}
})(jQuery);