Exclude model properties when syncing (Backbone.js)

后端 未结 11 1420
甜味超标
甜味超标 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:47

    In Underscore 1.3.3 they added pick and in 1.4.0 they added omit which can be used very simply to override your model's toJSON function to whitelist attributes with _.pick or blacklist attributes with _.omit.

    And since toJSON is used by the sync command for passing the data to the server I think this is a good solution as long as you do not want these fields wherever else you use toJSON.

    Backbone.Model.extend({
        blacklist: ['selected',],
        toJSON: function(options) {
            return _.omit(this.attributes, this.blacklist);
        },
    });
    

提交回复
热议问题