<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Backbone &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/backbone/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:18:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Backbone &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Backbone: model.destroy() Always Call &#8220;error&#8221; Callback Function</title>
		<link>https://myshittycode.com/2013/11/12/backbone-model-destroy-always-call-error-callback-function/</link>
					<comments>https://myshittycode.com/2013/11/12/backbone-model-destroy-always-call-error-callback-function/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 12 Nov 2013 21:45:44 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Spring MVC]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=225</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we invoke person.destroy() to delete a person:- &#8230; and let&#8217;s assume when person.destroy() is invoked, it will call /person/{personId} DELETE. Here&#8217;s how the Spring MVC controller API might look like:- When we execute person.destroy(), the error callback function will always get called even though the HTTP status is a 200 OK. Why? [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/11/12/backbone-model-destroy-always-call-error-callback-function/">Backbone: model.destroy() Always Call &#8220;error&#8221; Callback Function</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we invoke <code>person.destroy()</code> to delete a person:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
var person = Backbone.Model.extend( { ... } );

person.destroy( {
    contentType : &#039;application/json&#039;,

    success : function () {
        console.log(&#039;success&#039;);
    },

    error : function () {
        console.log(&#039;error&#039;);
    }
} );
</pre></div>


<p>&#8230; and let&#8217;s assume when <code>person.destroy()</code> is invoked, it will call <code>/person/{personId} DELETE</code>. Here&#8217;s how the Spring MVC controller API might look like:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Controller
public class PersonController {
    ...

    @RequestMapping(value = &quot;/person/{personId}&quot;, method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable Long personId) {

        Person person = personService.getPerson(personId);

        if (person == null) {
            return new ResponseEntity&amp;lt;UnexpectedErrorBean&amp;gt;(new UnexpectedErrorBean(&quot;Invalid person ID&quot;),
                                                           HttpStatus.BAD_REQUEST);
        }

        personService.remove(person);

        return new ResponseEntity(HttpStatus.OK);
    }
}
</pre></div>


<p>When we execute <code>person.destroy()</code>, the <code>error</code> callback function will always get called even though the HTTP status is a <code>200 OK</code>.</p>



<p>Why?</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>The reason is because Backbone expects a JSON payload from the web service call. In the example above, we return a <code>200 OK</code> but without a JSON payload. Thus, Backbone will treat this as an error and invoke the <code>error</code> callback function instead of <code>success</code> callback function regardless of the HTTP status.</p>



<p>There are three solutions to this problem.</p>



<h3 class="wp-block-heading">Solution 1: Add a JSON Payload</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [17]; title: ; notranslate">
@Controller
public class PersonController {
    ...

    @RequestMapping(value = &quot;/person/{personId}&quot;, method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable Long personId) {

        Person person = personService.getPerson(personId);

        if (person == null) {
            return new ResponseEntity&amp;lt;UnexpectedErrorBean&amp;gt;(new UnexpectedErrorBean(&quot;Invalid person ID&quot;),
                                                           HttpStatus.BAD_REQUEST);
        }

        personService.remove(person);

        return new ResponseEntity&amp;lt;Long&amp;gt;(personId, HttpStatus.OK);
    }
}
</pre></div>


<p>In this approach, we add a JSON payload to satisfy Backbone. In the above example, we return the value of <code>personId</code> back to the client. It can be an empty object too.</p>



<p>I don&#8217;t like this approach because that piece of information is useless to the client side. Even if I return an empty object, the solution seems very brittle because the next team member inheriting my code will not understand why there&#8217;s a need to return an empty object back to the client.</p>



<h3 class="wp-block-heading">Solution 2: Accept Payload as Text</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [6]; title: ; notranslate">
var person = Backbone.Model.extend( { ... } );

person.destroy( {
    contentType : &#039;application/json&#039;,

    dataType : &#039;text&#039;,

    success : function () {
        console.log(&#039;success&#039;);
    },

    error : function () {
        console.log(&#039;error&#039;);
    }
} );
</pre></div>


<p>Another approach is to accept the data as text from the server. This solution works fine if we are not expecting any complex data structure back from the server.</p>



<h3 class="wp-block-heading">Solution 3: Return a Different HTTP Status</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [17]; title: ; notranslate">
@Controller
public class PersonController {
    ...

    @RequestMapping(value = &quot;/person/{personId}&quot;, method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable Long personId) {

        Person person = personService.getPerson(personId);

        if (person == null) {
            return new ResponseEntity&amp;lt;UnexpectedErrorBean&amp;gt;(new UnexpectedErrorBean(&quot;Invalid person ID&quot;),
                                                           HttpStatus.BAD_REQUEST);
        }

        personService.remove(person);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}
</pre></div>


<p>The approach that I truly recommend is to return a <code>204 No Content</code> instead of <code>200 OK</code>. This way, the client side will not expect any payload from the server, and thus, Backbone will invoke the <code>success</code> callback function.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/11/12/backbone-model-destroy-always-call-error-callback-function/">Backbone: model.destroy() Always Call &#8220;error&#8221; Callback Function</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/11/12/backbone-model-destroy-always-call-error-callback-function/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">225</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-18 01:57:23 by W3 Total Cache
-->