Reputation: 189
After issuing delete command from Backbone sync, I am not getting sync event although Node server executes the command and deletes an entry from MongoDB. This is the relevant code on the frontend (Backbone):
handleDeleteCar: function (carId) {
Backbone.sync("delete", cars.get(carId), {url:'/api/cars/' + carId});
cars.remove(cars.get(carId));
},
and on the backend (Node):
app.delete('/api/cars/:id', function (req, res) {
var carImage = Car.findById(req.params.carId)._carImage;
Car.remove({ _id : req.params.id } , function (err){
if (!err){
console.log("Cars deleted");
Image.remove({_id:carImage}, function (err){
if (!err)
console.log("Image deleted");
res.json({"status":"O.K."});
});
}
});
});
As I said everything works fine except I am not getting a sync event in the browser. I am getting this event for every other verb I am using (get, post and put). I tried with different return values from the server, but to no avail.
Upvotes: 1
Views: 51
Reputation: 3091
You shouldn't be using Backbone.sync directly.
The correct way to do this is to set the collections url
to '/api/cars/' and call model.destroy()
:
var Cars = Backbone.Collection.extend({
url: '/api/cars/',
/* ... */
});
/* ... */
handleDeleteCar: function (carId) {
cars.get(carId).destroy()
}
Upvotes: 2