From b6b9ec72ff3edbe0005e6bd134ae18627c0ed703 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Sat, 16 Jun 2012 11:33:05 -0400 Subject: [PATCH] Fix #1365 - Destroy: new models execute success callback. --- backbone.js | 18 +++++++++--------- test/model.js | 7 +++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/backbone.js b/backbone.js index 834ed2f24..a066a355b 100644 --- a/backbone.js +++ b/backbone.js @@ -397,24 +397,24 @@ var model = this; var success = options.success; - var triggerDestroy = function() { + var destroy = function() { model.trigger('destroy', model, model.collection, options); }; - if (this.isNew()) { - triggerDestroy(); - return false; - } - options.success = function(resp) { - if (options.wait) triggerDestroy(); + if (options.wait || model.isNew()) destroy(); if (success) success(model, resp, options); - model.trigger('sync', model, resp, options); + if (!model.isNew()) model.trigger('sync', model, resp, options); }; + if (this.isNew()) { + options.success(); + return false; + } + options.error = Backbone.wrapError(options.error, model, options); var xhr = this.sync('delete', this, options); - if (!options.wait) triggerDestroy(); + if (!options.wait) destroy(); return xhr; }, diff --git a/test/model.js b/test/model.js index a23d732e1..9021b9d2d 100644 --- a/test/model.js +++ b/test/model.js @@ -840,4 +840,11 @@ $(document).ready(function() { model.destroy(); }); + test("#1365 - Destroy: New models execute success callback.", 2, function() { + new Backbone.Model() + .on('sync', function() { ok(false); }) + .on('destroy', function(){ ok(true); }) + .destroy({ success: function(){ ok(true); }}); + }); + });