[Pkg-javascript-commits] [backbone] 85/211: destroy of non-persisted model should not call sync

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:00:06 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 5a89ed327247f3047b0e79973a5990c2ae847216
Author: Raimonds Simanovskis <raimonds.simanovskis at gmail.com>
Date:   Tue Feb 15 15:17:20 2011 +0200

    destroy of non-persisted model should not call sync
---
 backbone.js        | 22 +++++++++++++---------
 test/collection.js | 11 +++++++++++
 test/model.js      |  7 +++++++
 3 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/backbone.js b/backbone.js
index 7a8ee41..bd58ead 100644
--- a/backbone.js
+++ b/backbone.js
@@ -286,18 +286,22 @@
       return this;
     },
 
-    // Destroy this model on the server. Upon success, the model is removed
+    // Destroy this model on the server if it was already persisted. Upon success, the model is removed
     // from its collection, if it has one.
     destroy : function(options) {
       options || (options = {});
-      var model = this;
-      var success = options.success;
-      options.success = function(resp) {
-        model.trigger('destroy', model, model.collection, options);
-        if (success) success(model, resp);
-      };
-      options.error = wrapError(options.error, model, options);
-      (this.sync || Backbone.sync).call(this, 'delete', this, options);
+      if (this.isNew()) {
+        this.trigger('destroy', this, this.collection, options);
+      } else {
+        var model = this;
+        var success = options.success;
+        options.success = function(resp) {
+          model.trigger('destroy', model, model.collection, options);
+          if (success) success(model, resp);
+        };
+        options.error = wrapError(options.error, model, options);
+        (this.sync || Backbone.sync).call(this, 'delete', this, options);
+      }
       return this;
     },
 
diff --git a/test/collection.js b/test/collection.js
index e620e13..2e66042 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -206,6 +206,17 @@ $(document).ready(function() {
     equals(null, e.collection);
   });
 
+  test("Colllection: non-persisted model destroy removes from all collections", function() {
+    var e = new Backbone.Model({title: 'Othello'});
+    e.sync = function(method, model, options) { throw "should not be called"; };
+    var colE = new Backbone.Collection([e]);
+    var colF = new Backbone.Collection([e]);
+    e.destroy();
+    ok(colE.length == 0);
+    ok(colF.length == 0);
+    equals(null, e.collection);
+  });
+
   test("Collection: fetch", function() {
     col.fetch();
     equals(lastRequest[0], 'read');
diff --git a/test/model.js b/test/model.js
index 7b4e9dd..626055a 100644
--- a/test/model.js
+++ b/test/model.js
@@ -276,6 +276,13 @@ $(document).ready(function() {
     ok(_.isEqual(lastRequest[1], doc));
   });
 
+  test("Model: non-persisted destroy", function() {
+    attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
+    a = new Backbone.Model(attrs);
+    a.sync = function(method, model, options) { throw "should not be called"; };
+    ok(a.destroy(), "non-persisted model should not call sync");
+  });
+
   test("Model: validate", function() {
     var lastError;
     var model = new Backbone.Model();

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