2

I have a backbone model like this

Report = Backbone.Model.extend({
  urlRoot: '/reports'
})

What I did:

model = new Report({id: 1, name: "Test Report1"});
model.urlRoot = "/reports/" + model.get('id') + "/submit";

model.save(null, {
  patch: true,
  success: function(model, res){
    console.log(res);
  },
  error: function(err){
    console.log(err);
  }
});

But when I save it I want it to send the request to a path like /reports/:id/submit with patch but it goes to a path like this /reports/1/submit/1 with POST. What can I do here? Any tweaks? Or should I use $.ajax instead?

Kumar
  • 3,116
  • 2
  • 16
  • 24

1 Answers1

0

Override url() function, not urlRoot.

Url is generated in function/parameter url.

model = new Report({id: 1, name: "Test Report1"});
oldUrl = model.url;
model.url = "/reports/" + model.get('id') + "/submit";

model.save(null, {
  patch: true,
  success: function(model, res){
    console.log(res);
  },
  error: function(err){
    console.log(err);
  }
});

model.url = oldUrl;
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49