This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlinePolls.js
100 lines (88 loc) · 2.87 KB
/
OnlinePolls.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
Polls = new Meteor.Collection('polls');
Results = new Meteor.Collection('results');
Router.map(function() {
this.route('OnlinePolls', {path: '/'});
this.route('about');
});
if (Meteor.isClient) {
Template.poll.helpers({
poll: function () {
return Polls.findOne();
},
count: function () {
return Polls.find().count();
}
});
Template.results.helpers({
result: function () {
return Results.find({}, {sort: {date: -1 }, limit: 10});
},
count: function () {
return Results.find().count();
}
});
Template.results.events({
'click a': function (e) {
e.preventDefault();
Results.remove({_id: this._id});
}
});
Template.poll.events({
'click input[type="submit"]': function (e) {
e.preventDefault();
var form = {};
var frm = $('.poll_form');
$.each(frm.serializeArray(), function () {
form[this.name] = this.value;
});
form['date'] = new Date().toTimeString();
form['userId'] = Meteor.userId();
var poll = Polls.findOne();
var index = null;
for (var i = 0; i < poll.options.length; i++) {
if (poll.options[i].name === form[this.name]) {
index = i;
break;
}
}
if (index !== null) {
Results.insert(form, function (err) {
if (!err) {
frm[0].reset();
} else {
alert("Something is wrong");
console.log(err);
}
});
var incModifier = { $inc: {} };
incModifier.$inc['options.' + index + '.value'] = 1;
Polls.update({_id: this._id}, incModifier);
}
},
'click .homeLink': function() {
Router.go('OnlinePolls');
if (window.console) window.console.log('home is here');
},
'click .aboutLink': function() {
Router.go('about');
if (window.console) window.console.log('about is here');
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
if (Polls.find().count() === 0) {
Polls.insert({
title: 'What is your gender',
title_ru: 'Выберите ваш пол',
name: 'gender',
options: [
{name: 'male', text: "I'm Male", value: 0},
{name: 'female', text: "I'm Female", value: 0 },
{name: 'idunno', text: "I don't know who am I", value: 0 }
]
});
}
});
}