-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequote.js
More file actions
106 lines (87 loc) · 2.32 KB
/
requote.js
File metadata and controls
106 lines (87 loc) · 2.32 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Quotes = new Mongo.Collection("quotes");
if (Meteor.isClient) {
Meteor.subscribe("quotes");
Template.home.helpers({
quotes: function(){
return Quotes.find({}, {sort: {createdAt: -1}});
}
});
Template.home.events({
"submit .new-quote": function(event){
var text = event.target.text.value;
var attribution = event.target.attribution.value;
var source = event.target.source.value;
var tags = event.target.tags.value;
Meteor.call("addQuote", text, attribution, source, tags);
event.target.text.value = "";
event.target.attribution.value = "";
event.target.source.value = "";
event.target.tags.value = "";
return false;
}
});
Template.quote.helpers({
isOwner: function() {
return this.owner === Meteor.userId();
}
});
Template.quote.events({
"click .delete": function(event){
Meteor.call("deleteQuote", this._id);
}
});
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}
Meteor.methods({
addQuote: function(text, attribution, source, tags){
if(! Meteor.userId()){
throw new Meteor.Error("not-authorized");
}
Quotes.insert({
text: text,
attribution: attribution,
source: source,
tags: tags,
createdAt: new Date(),
owner: Meteor.userId(),
ownername: Meteor.user().username
});
},
deleteQuote: function(quoteId){
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
var quote = Quotes.findOne(quoteId);
if (Meteor.userId() !== quote.owner){
throw new Meteor.Error("not-authorized")
}
Quotes.remove(quoteId);
}
})
if (Meteor.isServer) {
Meteor.publish("quotes", function(){
return Quotes.find();
});
}
UI.registerHelper('formatDate', function(date){
return moment(date).format('ddd MMM D YYYY');
});
Router.route('/', function(){
this.layout('base');
this.render('home');
});
Router.route('/quote/:_id', function(){
this.layout('base', {
data: function() {
return Quotes.findOne({_id: this.params._id})
}
});
this.render('singleQuote');
});
Router.route('/user/:username', function(){
this.layout('base');
var quotes = Quotes.find({ownername: this.params.username}, {sort: {createdAt: -1}});
this.render('userQuotes', {data: {quotes: quotes}});
});