<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<h1>Extend the Backbone.Model implementation and create an enhanced model with a new Backbone.EnhancedModel with an overridden sync method</h1>
<h2>(chrome) To see if this request is working, open your developer panel and click the network tab and run the code. Click on the delete request and inspect the Headers</h2>
<h3>You should see something like this.</h3>
<table>
<tr>
<th>Good Request</th>
<th>Bad Request (without backbone.sync override)</th>
</tr>
<tr>
<td><img src="http://i42.tinypic.com/5nkv1k.png"></img></td>
<td><img src="http://i43.tinypic.com/j1229v.png"></img></td>
</tr>
</table>
// Create your own 'enhanced' model
Backbone.EnhancedModel = Backbone.Model.extend({
destroy: function( options ) {
if ( options.data ) {
// properly formats data for back-end to parse
options.data = JSON.stringify(options.data);
}
// transform all delete requests to application/json
options.contentType = 'application/json';
Backbone.Model.prototype.destroy.call(this, options);
}
});
// Extend from the enhanced model so that you can get the overridden sync method
var Book = Backbone.EnhancedModel.extend({
urlRoot: 'api/book',
defaults: {
id: 1,
title: 'Backbone.js Help'
}
});
var someBook = new Book();
// Maybe you want to only delete hardcover copies from the db? Who knows...
// This usage is a less clean than just calling someBook.destroy(options)
someBook.destroy({
data: { cover: 'hardcover' }
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.