Exclude model properties when syncing (Backbone.js)

后端 未结 11 1419
甜味超标
甜味超标 2020-12-07 14:26

Is there a way to exclude certain property from my model when I sync?

For example, I keep in my model information about some view state. Let\'s say I have a picker m

11条回答
  •  悲&欢浪女
    2020-12-07 14:52

    This seems like the best solution (based on @nikoshr referenced question)

    Backbone.Model.extend({
    
        // Overwrite save function
        save: function(attrs, options) {
            options || (options = {});
            attrs || (attrs = _.clone(this.attributes));
    
            // Filter the data to send to the server
            delete attrs.selected;
            delete attrs.dontSync;
    
            options.data = JSON.stringify(attrs);
    
            // Proxy the call to the original save function
            return Backbone.Model.prototype.save.call(this, attrs, options);
        }
    });
    

    So we overwrite save function on the model instance, but we just filter out the data we don't need, and then we proxy that to the parent prototype function.

提交回复
热议问题