What is the least ugly way to force Backbone.sync updates to use POST instead of PUT?

前端 未结 5 1584
走了就别回头了
走了就别回头了 2020-12-05 14:29

Some of my Backbone models should always use POST, instead of POST for create and PUT for update. The server I persist these models to is capable of supporting all other ve

5条回答
  •  孤城傲影
    2020-12-05 14:50

    the way I've done it is to override sync() thusly

    Models.Thing = Backbone.Model.extend({
        initialize: function() {
            this.url = "/api/thing/" + this.id;
        },
        sync: function(method, model, options) {
            if (method === "read") method = "create";    // turns GET into POST
            return Backbone.sync(method, model, options);
        },
        defaults: {
            ...
    

提交回复
热议问题