Created
October 20, 2010 02:15
-
-
Save thejefflarson/635637 to your computer and use it in GitHub Desktop.
model validations on create
This file contains 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
// in Model: | |
Backbone.Model = function(attributes, options) { | |
this.attributes = {}; | |
this.cid = _.uniqueId('c'); | |
options = _.extend({silent:true}, options || {}); | |
this.set(attributes || {}, options); | |
this._previousAttributes = _.clone(this.attributes); | |
if (this.initialize) this.initialize(attributes); | |
}; | |
// in Collection: | |
_add : function(model, options) { | |
options || (options = {}); | |
if (!(model instanceof Backbone.Model)) { | |
model = new this.model(model, options); | |
} | |
var already = this.getByCid(model); | |
if (already) throw new Error(["Can't add the same model to a set twice", already.id]); | |
this._byId[model.id] = model; | |
this._byCid[model.cid] = model; | |
model.collection = this; | |
var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length; | |
this.models.splice(index, 0, model); | |
model.bind('all', this._boundOnModelEvent); | |
this.length++; | |
if (!options.silent) this.trigger('add', model); | |
return model; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment