Backbone.sync
This is responsible to handle connections between a RESTful server and the Backbone application is the Backbone.sync module. It transforms the fetch()
and save()
operations into HTTP requests:
fetch()
is mapped as aread
operation. This will makeGET
to the theurlRoot
attribute with the model ID for a model or theurl
attribute for a collection.save()
is mapped as acreate
orupdate
operation; it depends on theisNew()
method:This will be mapped as
create
if the model does not have an ID (isNew()
method returntrue
). A POST request is executed.This will be mapped as
update
if the model already has an ID (isNew()
method returnsfalse
). A PUT request is executed.
destroy()
is mapped as adelete
operation. This will make DELETE to the theurlRoot
attribute with the model ID for a model or theurl
attribute for a collection.
To better understand how Backbone.sync does its job, consider the following examples:
// read operation will issue a GET /contacts/1 varjohn= new Contact...