Skip to content

Commit

Permalink
Fix #1365 - Destroy: new models execute success callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Jun 16, 2012
1 parent 54ec530 commit b6b9ec7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
18 changes: 9 additions & 9 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
7 changes: 7 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); }});
});

});

0 comments on commit b6b9ec7

Please sign in to comment.