[Pkg-javascript-commits] [backbone] 133/211: Factor out model preparation to a separate function in Backbone.Collection, so 'create' and 'add' behave the same.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 17:00:13 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.5.0
in repository backbone.
commit 965c2992faa5627ebdbefcfde649b0661765cdd5
Author: Paul Uithol <paul.uithol at gmail.com>
Date: Tue Apr 26 10:36:28 2011 +0200
Factor out model preparation to a separate function in Backbone.Collection, so 'create' and 'add' behave the same.
---
backbone.js | 32 +++++++++++++++++++-------------
1 file changed, 19 insertions(+), 13 deletions(-)
diff --git a/backbone.js b/backbone.js
index bec4de0..70672f3 100644
--- a/backbone.js
+++ b/backbone.js
@@ -524,16 +524,12 @@
// Create a new instance of a model in this collection. After the model
// has been created on the server, it will be added to the collection.
+ // Returns the model, or 'false' if validation on a new model fails.
create : function(model, options) {
var coll = this;
options || (options = {});
- if (!(model instanceof Backbone.Model)) {
- var attrs = model;
- model = new this.model(null, {collection: coll});
- if (!model.set(attrs, options)) return false;
- } else {
- model.collection = coll;
- }
+ model = this._prepareModel(model, options);
+ if (!model) return false;
var success = options.success;
options.success = function(nextModel, resp, xhr) {
coll.add(nextModel);
@@ -564,20 +560,30 @@
this._byCid = {};
},
+ // Prepare a model to be added to this collection
+ _prepareModel: function(model, options) {
+ if (!(model instanceof Backbone.Model)) {
+ var attrs = model;
+ model = new this.model(null, {collection: this});
+ if (!model.set(attrs, options)) model = false;
+ }
+ else if (!model.collection) {
+ model.collection = this;
+ }
+ return model;
+ },
+
// Internal implementation of adding a single model to the set, updating
// hash indexes for `id` and `cid` lookups.
+ // Returns the model, or 'false' if validation on a new model fails.
_add : function(model, options) {
options || (options = {});
- if (!(model instanceof Backbone.Model)) {
- model = new this.model(model, {collection: this});
- }
+ model = this._prepareModel(model, options);
+ if (!model) return false;
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;
- if (!model.collection) {
- model.collection = this;
- }
var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length;
this.models.splice(index, 0, model);
model.bind('all', this._onModelEvent);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list