From e9e1abfe619a1d878fcd981161218d768f3a5dbf Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 21 Mar 2012 10:40:54 -0400 Subject: [PATCH] model.destroy() returns false if the model.isNew() --- backbone.js | 6 +++++- index.html | 8 ++++---- test/model.js | 3 +++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/backbone.js b/backbone.js index 5c87f55d0..ab40aaa8c 100644 --- a/backbone.js +++ b/backbone.js @@ -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) { diff --git a/index.html b/index.html index b11ea46c6..4e6696e02 100644 --- a/index.html +++ b/index.html @@ -1015,9 +1015,9 @@

Backbone.Model

destroymodel.destroy([options])
Destroys the model on the server by delegating an HTTP DELETE - request to Backbone.sync. Returns undefined - if the model isNew and a - jqXHR otherwise. Accepts + request to Backbone.sync. Returns a + jqXHR object, or + false if the model isNew. Accepts success and error callbacks in the options hash. Triggers a "destroy" event on the model, which will bubble up through any collections that contain it, and a "sync" event, after @@ -1649,7 +1649,7 @@

Backbone.Collection

Convenience to create a new instance of a model within a collection. Delegates to Backbone.sync and returns a jqXHR if validation - is successful; returns false otherwise. + is successful, and false 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 diff --git a/test/model.js b/test/model.js index 6bd24e42c..a20750467 100644 --- a/test/model.js +++ b/test/model.js @@ -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() {