Avoid parse after save

Backbone documentation says,

parse is called whenever a model's data is returned by the server, in fetch, and save. The function is passed the raw response object, and should return the attributes hash to be set on the model.

But i have customized parse function for my model. I want to execute it only when i fetch data not when i save data.

Is there a way to do it? I can check my response inside parse function. But is there any built-in option to do it?


This is from the backbone source file regarding saving a model:

var model = this;
var success = options.success;
options.success = function(resp) {
    model.attributes = attributes;
    var serverAttrs = model.parse(resp, options);
    if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
    if (_.isObject(serverAttrs) && !model.set(serverAttrs, options)) {
        return false;
    }
    if (success) success(model, resp, options);
    model.trigger('sync', model, resp, options);
};

You could pass a custom option on your save like: model.save(null, { saved: true }) , then in your custom parse :

parse: function(response, options) {
    if ( options.saved ) return this.attributes;
    // do what you're already doing
}

I haven't tested this at all, but it should at least get you started.


只需将一个parse:false传递给save方法即可。

m = new MyModel()
s.save(null, {parse: false})
链接地址: http://www.djcxy.com/p/67078.html

上一篇: 在主干中覆盖获取方法

下一篇: 保存后避免解析