-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFoodCal.js
122 lines (96 loc) · 2.91 KB
/
FoodCal.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
Meals = new Mongo.Collection('meals');
if (Meteor.isClient) {
Meteor.subscribe('meals');
Template.body.helpers({
meals: function () {
var meals = Meals.find({});
return meals;
}
});
Template.body.events({
'submit #new-meal': function(e) {
e.preventDefault();
var name = e.target.mealName.value;
var ingredient = e.target.mealIngredient.value;
Meteor.call('addMeal', name, ingredient);
e.target.mealName.value = '';
e.target.mealIngredient.value = '';
document.querySelector('#mealName').focus();
},
'click .delete-meal': function(e) {
e.preventDefault();
Meteor.call('deleteMeal', this._id);
},
'click .reorder-meals': function(e) {
e.preventDefault();
var meals = [];
$('.meal').each(function() {
meals.push(
this
)
});
for (var i = meals.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = meals[i];
meals[i] = meals[j];
meals[j] = temp;
}
meals.forEach(function(v) {
$('.meals').prepend(v);
})
},
'click .move-meal-right': function(e) {
e.preventDefault();
var meal = $('.meal[data-id="'+this._id+'"]');
var nextMeal = meal.next('.meal');
if (nextMeal.length > 0) {
nextMeal.after(meal);
}
},
'click .move-meal-left': function(e) {
e.preventDefault();
var meal = $('.meal[data-id="'+this._id+'"]');
var nextMeal = meal.prev('.meal');
if (nextMeal.length > 0) {
nextMeal.before(meal);
}
}
});
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
if (Meteor.isServer) {
Meteor.publish('meals', function() {
return Meals.find({
$or: [
{owner: this.userId}
]
});
})
}
});
}
Meteor.methods({
addMeal: function(name, ingredient) {
if (!Meteor.userId()) {
throw new Meteor.Error('not-authorized');
}
Meals.insert({
name: name,
ingredient: ingredient,
owner: Meteor.userId(),
username: Meteor.user().username,
createdAt: new Date()
});
},
deleteMeal: function(mealId) {
var meal = Meals.findOne(mealId);
if (meal.owner !== Meteor.userId()) {
throw new Meteor.Error('not-authorized');
}
Meals.remove(mealId);
}
});