-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththe-questionator.js
83 lines (68 loc) · 1.66 KB
/
the-questionator.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
Questions = new Mongo.Collection('questions');
Questions.allow({
insert: function(userId, doc) {
return !! userId;
}
});
Meteor.methods({
upvote: function(questionId) {
var question = Questions.findOne(questionId);
Questions.update(
questionId,
{ $set: { votes: question.votes + 1 }}
);
},
downvote: function(questionId) {
var question = Questions.findOne(questionId);
Questions.update(
questionId,
{ $set: { votes: question.votes - 1 }}
);
}
});
if (Meteor.isClient) {
Meteor.subscribe('questions');
Template.questionsList.helpers({
questions: Questions.find({}, {sort: {votes: -1}}),
});
Template.questionsList.events({
'click .vote-up': function(e) {
e.preventDefault();
Meteor.call('upvote', this._id);
},
'click .vote-down': function(e) {
e.preventDefault();
Meteor.call('downvote', this._id);
}
});
Template.questionForm.events({
'submit form': function(e) {
e.preventDefault();
var textarea = $(e.target).find('#question');
Questions.insert({
'text': textarea.val(),
'votes': 0
});
textarea.val('');
}
});
}
if (Meteor.isServer) {
if (Questions.find().count() === 0) {
Questions.insert({
text: 'Why does the sun shine?',
votes: 0
});
Questions.insert({
text: 'If you were a hot dog, and you were starving to death, would you eat yourself?',
votes: 0
});
Questions.insert({
text: 'What is the airspeed velocity of an unladen swallow?',
votes: 0
});
}
Meteor.publish('questions', function() {
return Questions.find();
});
}