How do I save just a subset of a Backbone Model's attributes to the server without triggering a PATCH instead of a POST

backbone.js, javascript

Solution

Below is a save override that can be placed in a base Backbone model or any individual model as appropriate. It may not cover every use case but it seems to fit this one.

JS Fiddle: http://jsfiddle.net/hEN88/1/

This override allows you to set a 'serverAttrs' array on any model to filter which properties are sent to the server.

save: function (attrs, options) { 
    attrs = attrs || this.toJSON();
    options = options || {};

    // If model defines serverAttrs, replace attrs with trimmed version
    if (this.serverAttrs) attrs = _.pick(attrs, this.serverAttrs);

    // Move attrs to options
    options.attrs = attrs;

    // Call super with attrs moved to options
    Backbone.Model.prototype.save.call(this, attrs, options);
}

Problem

The API I am hitting explodes if I send any "extra" properties back to the server. Now, I'm sure I've broken some MVC rules or something by having properties on my client side Backbone model that don't exist server side, but, what I need to do is initiate a CREATE request but only pass through some of the attributes from my model I am initiating the CREATE request from. I can easily do this in backbone: ``` Model.save({key: val}, {patch: true}); ``` And then modify the Backbone sync methodMap defaults patch routes to POST instead of PATCH and that nets me exactly what I'm looking for except that later on I want to actually be able to PATCH (instead of POST) when appropriate. I just need it to POST as if it were PATCHing for create actions only (not for update actions!). So, in short, I need to take something like this: ``` new Backbone.Model({'foo': 'bar', 'baz': 'beh'}); ``` And tell it to sync itself to the server, but ONLY send 'foo' across and not send 'baz', but it has to send it as a POST (it can't be a PATCH). Any ideas?

Original source

Related problems