destroy of non-persisted model should not call sync

This commit is contained in:
Raimonds Simanovskis
2011-02-15 15:17:20 +02:00
parent a5079aba1c
commit 5a89ed3272
3 changed files with 31 additions and 9 deletions

View File

@@ -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;
},

View File

@@ -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');

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