//backbone model connecte to server
var userModel = Backbone.Model.extend({
urlRoot: '/user',
defaults: {
name: '',
email: ''
},
initialize:function() {
console.log("welcome to backbone");
this.bind("error", function(model, error){
// We have received an error, log it, alert it or forget it :)
console.log("error");
console.log( error );
});
}
});
// Create new POST request for new recodr
var userData = {
name: "abc",
email: "test"
};
var user1 = new userModel();
// no ID set so post will be called to create new entry in table
user1.save(userData, {
success: function (user) {
console.log(user.toJSON());
}
});
// Create new GET request for fetching existing records
var user2 = new userModel({
id: 1
});
user2.fetch( {success: function (user) {
console.log(user.toJSON())
}});
// to update existing model fires PUT request
var user3 = new userModel({id:2});
user3.save({name:"kkr"},{
success:function(user){console.log(user.toJSON()); }
});
// to delete a record DELETE request
var user4 = new userModel({id:3});
user4.destroy({
success:function(user){console.log("destroyed");}
});
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.
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!
Console
Debug your Fiddle with a minimal built-in JavaScript console.