Skip to content

Instantly share code, notes, and snippets.

@simistern
Created February 22, 2019 15:30
Show Gist options
  • Save simistern/3fa85ab4fb9958e523953900a7358a55 to your computer and use it in GitHub Desktop.
Save simistern/3fa85ab4fb9958e523953900a7358a55 to your computer and use it in GitHub Desktop.
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);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment