Skip to content

Instantly share code, notes, and snippets.

@adatta02
Created February 9, 2013 00:00
Show Gist options
  • Save adatta02/4743008 to your computer and use it in GitHub Desktop.
Save adatta02/4743008 to your computer and use it in GitHub Desktop.
var Language = Backbone.Model.extend({
defaults: function() {
return {
languageName: "",
};
},
initialize: function() {
},
url: "echoBackbone.php",
sync: function(method, model, options){
var methodMap = {
'create': 'POST',
'update': 'PUT',
'delete': 'DELETE',
'read': 'GET'
};
var type = methodMap[method];
// Default options, unless specified.
options || (options = {});
// Default JSON-request options.
var params = {type: type, dataType: 'json'};
// Ensure that we have a URL.
if (!options.url) {
params.url = this.url || urlError();
}
// Don't process data on a non-GET request.
if (params.type !== 'GET' && !Backbone.emulateJSON) {
params.processData = false;
}
// NEW - the juicy bits to combine all the models in this collection
var collection = [];
model.collection.each( function(e, i, l){
collection.push( e.toJSON() );
});
params.data = JSON.stringify(collection);
// Make the request, allowing the user to override any Ajax options.
return $.ajax(_.extend(params, options));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment