[Pkg-javascript-commits] [backbone] 174/211: Merging in #232. Allow destroy() to be called on non-persisted objects ... just in case.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:00:20 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 e01ad9dd5b685603ac80b8a0ed116e0711055cc1
Merge: 87a81e4 5a89ed3
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Fri May 27 11:30:48 2011 -0400

    Merging in #232. Allow destroy() to be called on non-persisted objects ... just in case.

 backbone.js        |  3 ++-
 test/collection.js | 15 +++++++++++++--
 test/model.js      |  7 +++++++
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --cc backbone.js
index 2b98b1f,bd58ead..8833384
--- a/backbone.js
+++ b/backbone.js
@@@ -301,21 -282,27 +301,22 @@@
        };
        options.error = wrapError(options.error, model, options);
        var method = this.isNew() ? 'create' : 'update';
 -      (this.sync || Backbone.sync).call(this, method, this, options);
 -      return this;
 +      return (this.sync || Backbone.sync).call(this, method, this, options);
      },
  
-     // 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 = {});
 -      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;
++      if (this.isNew()) return this.trigger('destroy', this, this.collection, 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);
 +      return (this.sync || Backbone.sync).call(this, 'delete', this, options);
      },
  
      // Default URL for the model's representation on the server -- if you're
diff --cc test/collection.js
index 7e9d770,2e66042..dfbfbad
--- a/test/collection.js
+++ b/test/collection.js
@@@ -83,16 -83,6 +83,16 @@@ $(document).ready(function() 
      equals(otherCol.length, 1);
      equals(secondAdded, null);
      ok(opts.amazing);
-     
++
 +    var f = new Backbone.Model({id: 20, label : 'f'});
 +    var g = new Backbone.Model({id: 21, label : 'g'});
 +    var h = new Backbone.Model({id: 22, label : 'h'});
 +    var atCol = new Backbone.Collection([f, g, h]);
 +    equals(atCol.length, 3);
 +    atCol.add(e, {at: 1});
 +    equals(atCol.length, 4);
 +    equals(atCol.at(1), e);
 +    equals(atCol.last(), h);
    });
  
    test("Collection: add model to multiple collections", function() {
@@@ -205,7 -195,7 +205,7 @@@
      equals(counter, 2);
    });
  
--  test("Colllection: model destroy removes from all collections", function() {
++  test("Collection: model destroy removes from all collections", function() {
      var e = new Backbone.Model({id: 5, title: 'Othello'});
      e.sync = function(method, model, options) { options.success({}); };
      var colE = new Backbone.Collection([e]);
diff --cc test/model.js
index 34754f7,626055a..2f68a5c
--- a/test/model.js
+++ b/test/model.js
@@@ -276,6 -276,13 +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"; };
++    a.sync = function() { 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