Created
June 30, 2012 20:52
-
-
Save ajsharp/3025465 to your computer and use it in GitHub Desktop.
backbone 0.9.2 Model validations -- both set and save call _validate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _validate: function(attrs, options) { | |
| if (options.silent || !this.validate) return true; | |
| attrs = _.extend({}, this.attributes, attrs); | |
| var error = this.validate(attrs, options); | |
| if (!error) return true; | |
| if (options && options.error) { | |
| options.error(this, error, options); | |
| } else { | |
| this.trigger('error', this, error, options); | |
| } | |
| return false; | |
| }, | |
| // Set a hash of model attributes, and sync the model to the server. | |
| // If the server returns an attributes hash that differs, the model's | |
| // state will be `set` again. | |
| save: function(key, value, options) { | |
| var attrs, current; | |
| // Handle both `("key", value)` and `({key: value})` -style calls. | |
| if (_.isObject(key) || key == null) { | |
| attrs = key; | |
| options = value; | |
| } else { | |
| attrs = {}; | |
| attrs[key] = value; | |
| } | |
| options = options ? _.clone(options) : {}; | |
| // If we're "wait"-ing to set changed attributes, validate early. | |
| if (options.wait) { | |
| if (!this._validate(attrs, options)) return false; | |
| current = _.clone(this.attributes); | |
| } | |
| // Regular saves `set` attributes before persisting to the server. | |
| var silentOptions = _.extend({}, options, {silent: true}); | |
| if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) { | |
| return false; | |
| } | |
| // After a successful server-side save, the client is (optionally) | |
| // updated with the server-side state. | |
| var model = this; | |
| var success = options.success; | |
| options.success = function(resp, status, xhr) { | |
| var serverAttrs = model.parse(resp, xhr); | |
| if (options.wait) { | |
| delete options.wait; | |
| serverAttrs = _.extend(attrs || {}, serverAttrs); | |
| } | |
| if (!model.set(serverAttrs, options)) return false; | |
| if (success) { | |
| success(model, resp); | |
| } else { | |
| model.trigger('sync', model, resp, options); | |
| } | |
| }; | |
| // Finish configuring and sending the Ajax request. | |
| options.error = Backbone.wrapError(options.error, model, options); | |
| var method = this.isNew() ? 'create' : 'update'; | |
| var xhr = (this.sync || Backbone.sync).call(this, method, this, options); | |
| if (options.wait) this.set(current, silentOptions); | |
| return xhr; | |
| }, | |
| // Set a hash of model attributes on the object, firing `"change"` unless | |
| // you choose to silence it. | |
| set: function(key, value, options) { | |
| var attrs, attr, val; | |
| // Handle both | |
| if (_.isObject(key) || key == null) { | |
| attrs = key; | |
| options = value; | |
| } else { | |
| attrs = {}; | |
| attrs[key] = value; | |
| } | |
| // Extract attributes and options. | |
| options || (options = {}); | |
| if (!attrs) return this; | |
| if (attrs instanceof Model) attrs = attrs.attributes; | |
| if (options.unset) for (attr in attrs) attrs[attr] = void 0; | |
| // Run validation. | |
| if (!this._validate(attrs, options)) return false; | |
| // Check for changes of `id`. | |
| if (this.idAttribute in attrs) this.id = attrs[this.idAttribute]; | |
| var changes = options.changes = {}; | |
| var now = this.attributes; | |
| var escaped = this._escapedAttributes; | |
| var prev = this._previousAttributes || {}; | |
| // For each `set` attribute... | |
| for (attr in attrs) { | |
| val = attrs[attr]; | |
| // If the new and current value differ, record the change. | |
| if (!_.isEqual(now[attr], val) || (options.unset && _.has(now, attr))) { | |
| delete escaped[attr]; | |
| (options.silent ? this._silent : changes)[attr] = true; | |
| } | |
| // Update or delete the current value. | |
| options.unset ? delete now[attr] : now[attr] = val; | |
| // If the new and previous value differ, record the change. If not, | |
| // then remove changes for this attribute. | |
| if (!_.isEqual(prev[attr], val) || (_.has(now, attr) != _.has(prev, attr))) { | |
| this.changed[attr] = val; | |
| if (!options.silent) this._pending[attr] = true; | |
| } else { | |
| delete this.changed[attr]; | |
| delete this._pending[attr]; | |
| } | |
| } | |
| // Fire the `"change"` events. | |
| if (!options.silent) this.change(options); | |
| return this; | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment