Created
August 1, 2014 06:00
-
-
Save panw/2ce6be614afd678b638c to your computer and use it in GitHub Desktop.
For part 5 of the tutorial. Code was added to give users the functionality to add items to the list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Items = new Meteor.Collection('items'); | |
if(Meteor.isClient){ | |
Template.groceryList.events({ | |
'click li' : function(event, template){ | |
var listItem = event.currentTarget; // get the list item being clicked on | |
$('#'+listItem.id).hide(); // hide li with matching id | |
// update check attribute for the item in the database | |
Items.update(listItem.id, { $set : {checked:true} }, function(error){ | |
// if error occurs print it out | |
if(error){ | |
console.log(error.reason); | |
} | |
}); | |
}, | |
'click #add-button' : function(event, template) { | |
// prevent button's default action | |
event.preventDefault(); | |
// get the new item's name | |
var newItem = $("#new-item").val(); | |
// add new item to the database | |
Items.insert({ name:newItem, checked:false }); | |
// clear input field once we have stored the data | |
$("#new-item").val(""); | |
} | |
}); | |
Template.groceryList.helpers({ | |
items: function() { | |
return Items.find({checked:false}); | |
} | |
}); | |
} | |
if(Meteor.isServer){ | |
if (Items.find().count() === 0) { | |
Items.insert({ name: "Baguette", checked: false }); | |
Items.insert({ name: "Butter", checked: false }); | |
Items.insert({ name: "Jam", checked: false }); | |
Items.insert({ name: "Coconut", checked: false }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment