From 1ce85e3833bb00769f1d6af71e0ea8c3ac8faba6 Mon Sep 17 00:00:00 2001 From: visumdesignz Date: Fri, 14 Mar 2014 04:41:38 -0500 Subject: [PATCH 1/4] Update contact_details.js Added a way to destroy() the model inside the gulp.js branch version. --- client/src/views/contact_details.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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(); } }); From e36c1846b7cc904d2aa17c0caa2419fe0f8da5a2 Mon Sep 17 00:00:00 2001 From: visumdesignz Date: Fri, 14 Mar 2014 04:43:06 -0500 Subject: [PATCH 2/4] Update contact_details.hbs Updated this template to add a button so the deleteContact() method would work. https://github.com/visumdesignz/benm/blob/gulp/client/src/views/contact_details.js --- client/templates/contact_details.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 13bd9745c658bf8d236bda39d61c64509c95d014 Mon Sep 17 00:00:00 2001 From: visumdesignz Date: Fri, 14 Mar 2014 04:45:39 -0500 Subject: [PATCH 3/4] Update routes.js Added a delete route so that deleteContact() inside client/src/views/contact_details.js would send a delete request. --- app/routes.js | 1 + 1 file changed, 1 insertion(+) 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); }; From fb5bc9ae747547f5423d601823018f5e477c588f Mon Sep 17 00:00:00 2001 From: visumdesignz Date: Fri, 14 Mar 2014 04:46:55 -0500 Subject: [PATCH 4/4] Update contacts.js Added delete so that I could get this gulp version to send delete requests. --- controllers/contacts.js | 11 +++++++++++ 1 file changed, 11 insertions(+) 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'}); + }) + } + }); } };