3

What is the difference between 'sync' event and Backbone.sync?

...and what are they each specifically?

Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63

1 Answers1

3

There is no connection between the two, although they are both related to the task of syncing data to the server.

Backbone.sync implements the persistence strategy utilized by your Backbone.js app. If you need to change something about the way your data is stored, you are welcome to override the default implementation. Most people won't need to worry about this, but if you need to do this you might want to check out How to override Backbone.sync?

The sync event indicates an update to a model has been successfully synchronized with the server. Like other model events, it will bubble up to the collection as well. This happens when:

Note: Prior to v0.9.9, the sync event was not guaranteed to fire - if you declared a success callback during any one of these operations, the sync event would not be triggered.

Community
  • 1
  • 1
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
  • this is a particularly fragile part of Backbone. nowhere in the docs does it mention that 'sync' may not fire, but you're completely right, Brad -- it's easy to set up your own response handlers and neglect to trigger('sync'). i'm working with a request framework written by someone else, and it took me an hour to figure out this was omitted. – ericsoco Jul 17 '13 at 01:09
  • I'm not sure if it's accurate that "if you declare a success callback during any of these operations, the sync event will not be triggered". Looking at the source http://backbonejs.org/docs/backbone.html#section-60, your success callback gets wrapped in a callback that triggers sync. – Geoffrey Hing Oct 21 '13 at 23:04
  • 1
    Looks like you're right. This behavior was [changed](http://backbonejs.org/#changelog) in Backbone 0.9.9. – Brad Koch Oct 21 '13 at 23:46