Backbonejs - 保存后避免解析

use*_*r10 5 backbone.js

骨干文档说,

只要服务器返回模型的数据,在获取和保存时,就会调用parse.该函数传递原始响应对象,并应返回要在模型上设置的属性哈希.

但我为我的模型定制了解析功能.我想只在我获取数据时才执行它,而不是在我保存数据时.

有办法吗?我可以在解析函数中检查我的响应.但是有没有内置的选项呢?

kal*_*ley 7

这是关于保存模型的主干源文件:

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);
};
Run Code Online (Sandbox Code Playgroud)

您可以在您的save喜欢上传递自定义选项:model.save(null, { saved: true }),然后在您的自定义中parse:

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

我根本没有测试过这个,但至少应该让你开始.