Backbone's fetch/save is simpler than you think

I had an epiphany yesterday: Backbone's fetch and save methods are ridiculously simple. I've used backbone with a backend before, and the data persistence aspect was always this nebulous grey area in my mental model.

So how simple is it?

  1. Set the url property of your model to a REST endpoint that returns a json object.
  2. Call the model's fetch method.

And that's it. The default Backbone.sync method uses jQuery's ajax module under the hood to make ajax calls to the endpoint you've specified, and updates your model's data accordingly. It will even perform any client-side validation you've set up through that model's validate method.

As a bonus, backbone is setup to allow you to overwrite/extend the Backbone.sync method if you need to perform server-side validation, authorization, error-handling, or other fancy things like web-sockets.