-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (42 loc) · 1.4 KB
/
index.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
function ChoiceViewModel(parent, data) {
var self = this;
self.data = data;
self.selected = ko.observable(false);
self.click = function() {
self.selected(!self.selected());
window.location.hash = data.protein_common_name;
};
};
function IndexViewModel() {
var self = this;
self.choiceModels = ko.computed(function() {
var foodChoices = protein;
var choices = [];
foodChoices.sort(function(a, b) {
return a.protein_common_name.localeCompare(b.protein_common_name)
});
for (var i = 0; i < foodChoices.length; i++) {
choices.push(new ChoiceViewModel(self, foodChoices[i]));
}
return choices;
});
self.foodGrid = ko.computed(function () {
var foodChoices = self.choiceModels();
var groupedChoices = [];
for (var i = 0; i < foodChoices.length; i += 2) {
groupedChoices.push([foodChoices[i], i + 1 < foodChoices.length ? foodChoices[i + 1] : null]);
}
return groupedChoices;
});
self.choiceMade = ko.computed(function () {
var choices = self.choiceModels();
var selections = [];
for (var i = 0; i < choices.length; i++) {
if (choices[i].selected()) {
selections.push(choices[i]);
}
}
return selections;
});
}
ko.applyBindings(new IndexViewModel());