The detailed process can be found in the annotated source code for Backbone.Model.save and Backbone.sync.
If you ignore options.wait
and options.silent
, your decomposition is mostly correct.
When you issue a model.save
:
- the attributes passed to the function are set, a change event is fired if the values changed
save
delegates the request to model.sync
or Backbone.sync
sync
serializes the data to a JSON string by calling JSON.stringify(model.toJSON())
- An Ajax request is sent to sent to server, a POST request for a new object, a PUT for an update. The target URL is defined by
model.url
(or collection.url/id
)
- When the request completes, the model is updated with the server response, if any, and triggers a
change
event accordingly.
- Success or error callbacks are called, a
sync
event is triggered if no success callback is defined.
Usually, you can customize this behaviour by overriding model.toJSON
or model.sync