PATCH request method in Backbone.js

后端 未结 3 1223
星月不相逢
星月不相逢 2021-02-07 04:34

What is the right way to perform PATCH request while saving model\'s attributes in Backbone.js?

3条回答
  •  [愿得一人]
    2021-02-07 05:01

    You'll have to override Backbone.sync and extend the existing method mapper

    var methodMap = {
        'create': 'POST',
        'update': 'PUT',
        'delete': 'DELETE',
        'read':   'GET',
        'patch':  'PATCH'
    };
    

    you'll have to create your own patch method on a model like

    Backbone.Model.prototype.patch = function(options) 
    {
        // some code here that checks what attributes have changed since last save
        var xhr = (this.sync || Backbone.sync).call(this, 'patch', this, options);
        return xhr;  
    }
    

    I'm sure you can extend Backbone further to include OPTIONS and HEAD if you needed to

    Note though, that even through jQuery supports the PATCH, OPTIONS and HEAD methods, your end-users' browser may not.

提交回复
热议问题