[Pkg-javascript-commits] [backbone] 167/211: Updated to apply on the 0.5.0 branch

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:00:19 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 d7e3d5bf4ad8ebf8482c873d3742a7b21580a59e
Author: Paul Uithol <paul.uithol at gmail.com>
Date:   Thu May 26 21:34:09 2011 +0200

    Updated to apply on the 0.5.0 branch
---
 backbone.js | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/backbone.js b/backbone.js
index 735fc9c..c2e5618 100644
--- a/backbone.js
+++ b/backbone.js
@@ -521,16 +521,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, options);
@@ -553,7 +549,7 @@
       return _(this.models).chain();
     },
 
-    // Reset all internal state. Called when the collection is reset.
+    // Reset all internal state. Called when the collection is refreshed.
     _reset : function(options) {
       this.length = 0;
       this.models = [];
@@ -561,20 +557,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 = options.at != null ? options.at :
                   this.comparator ? this.sortedIndex(model, this.comparator) :
                   this.length;

-- 
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