Models
AJAX Abstraction for a Single Resource
AJAX Abstraction for a Single Resource
The save() method is used to create or update a resource, depending on whether the model has an id.
You can learn about how this method can be used to create resources here, and about how it can be used to update resources here.
save: function(key, val, options) {
const model = this;
let attrs;
if (key == null || typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
options = _.extend({ validate: true, parse: true }, options);
if (attrs) {
if (!this.set(attrs, options)) {
return false
};
} else if (!this._validate(attrs, options)) {
return false;
}
options.success = function(attributes) {
if (options.parse) {
attributes = model.parse(attributes, options);
}
if (attributes && !model.set(attributes, options)) {
return false;
}
};
const method = this.isNew() ? 'create' : (
options.patch ? 'patch' : 'update'
);
if (method === 'patch' && !options.attrs) {
options.attrs = attrs
};
return this.sync(method, this, options);
},