I've been looking through multiple posts about how to save a Backbone collection using a non-RESTful server and I'm still a little confused. I've created a collection where I've overridden the toJSON
method to customise my data for posting to my API ("/api/entity/735/request/personDelete"
currently swapped out for jsfiddles /echo/json). So I've created a save
method which uses Backbone.sync
, on success
I'm logging out any kind of response and the object is empty, not sure where things get lost or what I'm doing wrong; can anyone give me some guidance? Would just like to get this example working so I can use something like this going forward.
JS
var PersonCollection = Backbone.Collection.extend({
model: PersonModel,
url: function() {
// Dummy JSFiddle endpoint
// Example non-RESTful url "/api/entity/735/request/personDelete"
return '/echo/json/';
},
/**
* Override toJSON to loop through collection models making
* custom objects containing specific attributes to be posted.
*/
toJSON: function() {
console.log(this.models);
var plucked = this.models.map(function(model) {
return _.pick( model.toJSON(), ["id","name", "teams"] )
});
console.log(plucked);
return plucked;
},
save: function(options) {
Backbone.sync('create', this, {
success: function(data, textStatus, jqXHR) {
console.log('Saved!', data);
}
});
}
});
JSFiddle: http://jsfiddle.net/kyllle/f1h4cz7f/3/