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

This commit is contained in:
Jeremy Ashkenas
2011-05-27 11:30:48 -04:00
3 changed files with 22 additions and 3 deletions

View File

@@ -304,10 +304,11 @@
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()) return this.trigger('destroy', this, this.collection, options);
var model = this;
var success = options.success;
options.success = function(resp) {

View File

@@ -83,7 +83,7 @@ $(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'});
@@ -205,7 +205,7 @@ $(document).ready(function() {
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]);
@@ -216,6 +216,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');

View File

@@ -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() { 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();