documentcloud/backbone — Issue #1019: Model.parse needs a compliment: Model.serialize
Author: BenLambert
Created: 2012-02-17 04:09:10+00:00
State: open

So Backbone has a great function for parsing data from preexisting APIs unto something more suitable for the Backbone model.

But it is only half of the equation. We also need to serialize the data back into the format required by the server.

Right now if you are making use of Model.parse, you basically also have to write your own custom `sync` method. That's not entirely bad, but it could be much simpler if Backbone included a `serialize` method on the model that was called by the default Backbone.sync method.

Basically it would just mean changing this line in Backbone.sync
params.data = JSON.stringify(model.toJSON());
to this:
params.data = JSON.stringify(model.serialize.call(model, model.toJSON()));
and then updating the Backbone.Model to include a new no-op `serialize` method just like `parse`.
parse: function(resp, xhr) {
return resp;
},
serialize: function(attrs) {
return attrs;
},