Reputation: 2034
I've been trying to implement a use-case where I've a collection of email list. Following is a JSON response retrieved from the backend:
{ "emails": [
{
"action": null,
"emailType": null,
"emailAddress": "[email protected]"
},
{
"action": null,
"emailType": "EML1",
"emailAddress": "[email protected]"
}]}
When deleting an email, I
this.collection.remove(this.emailModel);
All this does is removes the deleted model from the collection. Instead I'd like to not remove but add an action = "Delete" to this deleted model such that the UPDATE request payload becomes something like this:
{ "emails": [
{
"action": null,
"emailType": null,
"emailAddress": "[email protected]"
},
{
"action": "Delete",
"emailType": "EML1",
"emailAddress": "[email protected]"
}]}
Can anyone suggestme a way to go about it? Thanks in advance!
Upvotes: 0
Views: 387
Reputation: 43156
For deleting model, instead of
this.collection.remove(this.emailModel);
Do
this.emailModel.destroy();
That'll send a DELETE
request and remove the model from collection.
To answer your question:
You can do this.emailModel.set("action","Delete");
and then call .save()
on the model, which will send a POST/UPDATE
request with the model data depending on the state of model. You can override models sync
method to always send POST
if your backend also doesn't support UPDATE
. When the save
is success, remove the model from collection at client side. Ideally your backend should be restful for easy integration with backbone.
Upvotes: 1