contactbook.js
Contacts = new Mongo.Collection('contacts');
var checked = [];
if (Meteor.isClient) {
Template.contacts.helpers({
contacts: function(){
var filter = {sort: {}};
var query = Session.get('query');
filter.sort[Session.get('sortfilter')] = 1;
return Contacts.find({name: new RegExp(query, 'i'),user_id: Meteor.userId() }, filter);
}
});
Template.new_contact.events({
"submit .contactForm": function(event){
var id = Session.get('is_editing');
if (id !== "") {
Contacts.update(id, { $set: {
name: event.target.name.value,
number: event.target.number.value,
email: event.target.email.value,
address: event.target.address.value,
user_id: Meteor.userId()
}
});
Session.set('is_editing', '');
} else {
Contacts.insert({
name: event.target.name.value,
number: event.target.number.value,
email: event.target.email.value,
address: event.target.address.value,
user_id: Meteor.userId(),
createdAt: new Date()
});
event.target.name.value = "";
event.target.number.value = "";
event.target.email.value = "";
event.target.address.value = "";
}
return false;
}
});
Template.navbar.events({
"click .delete": function () {
$.each(checked, function(index, value) {
Contacts.remove(value);
});
checked = [];
},
"keyup .searchbox": function(event){
var query = event.target.value;
//console.log(query);
Session.set('query', query);
}
});
Template.contacts.events({
"click th": function(event){
// print which table header was clicked
//console.log($(event.target).text());
var order = $(event.target).text().toLowerCase();
Session.set('sortfilter', order);
},
"click .toggle-checked": function () {
var index = checked.indexOf(this._id);
if (index > -1) {
checked.splice(index, 1);
}else{
checked.push(this._id);
}
}
});
Template.contact.events({
"click .update": function (event) {
$(".contactForm [name='name']").val(this.name);
$(".contactForm [name='number']").val(this.number);
$(".contactForm [name='email']").val(this.email);
$(".contactForm [name='address']").val(this.address);
Session.set('is_editing', this._id);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}