diff --git a/app/routes.js b/app/routes.js
index 2579361..662e1fc 100644
--- a/app/routes.js
+++ b/app/routes.js
@@ -7,4 +7,5 @@ module.exports.initialize = function(app) {
app.get('/api/contacts/:id', contacts.getById);
app.post('/api/contacts', contacts.add);
app.put('/api/contacts', contacts.update);
+ app.delete('/api/contacts/:id', contacts.delete);
};
diff --git a/client/src/views/contact_details.js b/client/src/views/contact_details.js
index 9243423..71a2992 100644
--- a/client/src/views/contact_details.js
+++ b/client/src/views/contact_details.js
@@ -3,11 +3,25 @@ var Marionette = require('backbone.marionette');
module.exports = ContactDetailsView = Marionette.ItemView.extend({
template: require('../../templates/contact_details.hbs'),
events: {
- 'click a': 'goBack'
+ 'click a.back': 'goBack',
+ 'click a.delete': 'deleteContact'
},
goBack: function(e) {
e.preventDefault();
+ window.App.controller.home();
+ },
+ deleteContact: function(e) {
+ e.preventDefault();
+ console.log('Deleting contact');
+
+ // this will actually send a DELETE to the server:
+ this.model.destroy({
+ success: function(model, response) {
+ window.App.data.contacts.remove(this.model);
+ }
+ });
+
window.App.controller.home();
}
});
diff --git a/client/templates/contact_details.hbs b/client/templates/contact_details.hbs
index 37de510..88dda1c 100644
--- a/client/templates/contact_details.hbs
+++ b/client/templates/contact_details.hbs
@@ -7,4 +7,4 @@
-<< Back
+<< Back | Delete Contact
diff --git a/controllers/contacts.js b/controllers/contacts.js
index 020de2d..0451be0 100644
--- a/controllers/contacts.js
+++ b/controllers/contacts.js
@@ -27,5 +27,16 @@ module.exports = {
update: function(req, res) {
console.log(req.body);
res.json(req.body);
+ },
+ delete: function(req, res) {
+ models.Contact.findOne({ _id: req.params.id }, function(err, contact) {
+ if (err) {
+ res.json({error: 'Contact not found.'});
+ } else {
+ contact.remove(function(err, contact){
+ res.json(200, {status: 'Success'});
+ })
+ }
+ });
}
};