使用非restful api保存主干模型

Backbone model save with non restful api

本文关键字:模型 保存 api restful      更新时间:2023-09-26

我有一个这样的主干模型

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

我做了什么:

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);
  }
});

但是当我保存它时,我希望它将请求发送到/reports/:id/submitpatch这样的路径,但它会发送到/reports/1/submit/1POST这样的路径。我在这里能做什么?调整吗?或者我应该使用$.ajax代替?

覆盖url()功能,不覆盖urlRoot

Url在函数/参数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;