Skip to content

Commit

Permalink
model.destroy() returns false if the model.isNew()
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Mar 21, 2012
1 parent a554d6b commit e9e1abf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
6 changes: 5 additions & 1 deletion backbone.js
Expand Up @@ -377,7 +377,11 @@
model.trigger('destroy', model, model.collection, options);
};

if (this.isNew()) return triggerDestroy();
if (this.isNew()) {
triggerDestroy();
return false;
}

options.success = function(resp) {
if (options.wait) triggerDestroy();
if (success) {
Expand Down
8 changes: 4 additions & 4 deletions index.html
Expand Up @@ -1015,9 +1015,9 @@ <h2 id="Model">Backbone.Model</h2>
<b class="header">destroy</b><code>model.destroy([options])</code>
<br />
Destroys the model on the server by delegating an HTTP <tt>DELETE</tt>
request to <a href="#Sync">Backbone.sync</a>. Returns <tt>undefined</tt>
if the model <a href="#Model-isNew">isNew</a> and a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a> otherwise. Accepts
request to <a href="#Sync">Backbone.sync</a>. Returns a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a> object, or
<tt>false</tt> if the model <a href="#Model-isNew">isNew</a>. Accepts
<tt>success</tt> and <tt>error</tt> callbacks in the options hash.
Triggers a <tt>"destroy"</tt> event on the model, which will bubble up
through any collections that contain it, and a <tt>"sync"</tt> event, after
Expand Down Expand Up @@ -1649,7 +1649,7 @@ <h2 id="Collection">Backbone.Collection</h2>
Convenience to create a new instance of a model within a collection.
Delegates to <a href="#Sync">Backbone.sync</a> and returns a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a> if validation
is successful; returns <tt>false</tt> otherwise.
is successful, and <tt>false</tt> otherwise.
Equivalent to instantiating a model with a hash of attributes,
saving the model to the server, and adding the model to the set after being
successfully created. Returns
Expand Down
3 changes: 3 additions & 0 deletions test/model.js
Expand Up @@ -376,6 +376,9 @@ $(document).ready(function() {
doc.destroy();
equal(lastRequest.method, 'delete');
ok(_.isEqual(lastRequest.model, doc));

var newModel = new Backbone.Model;
equal(newModel.destroy(), false);
});

test("Model: non-persisted destroy", function() {
Expand Down

0 comments on commit e9e1abf

Please sign in to comment.