What exactly happens when you save a Backbone model? Here's the best I can piece together by reading the documentation here:
model.save([attributes], [options])
is calledBut I'm a Backbone noob and I'm sure someone else could do a way better job of explaining.
I'm partly just curious what happens. I'm also having trouble understanding how Backbone comes up with the JSON object it sends to the server. I'm having a separate problem where the JSON object is not what I want it to be, but I don't know how to change it.
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
:
save
delegates the request to model.sync
or Backbone.sync
sync
serializes the data to a JSON string by calling JSON.stringify(model.toJSON())
model.url
(or collection.url/id
)change
event accordingly.sync
event is triggered if no success callback is defined.Usually, you can customize this behaviour by overriding model.toJSON
or model.sync
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With