Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone model sync status

In my app, the user creates a collection with a bunch of models. As I don't want to make a ton of requests to the server, I've created a custom function that saves these models in batch to the server. The server then responds with all the models including their id, and this is then set to the various models. All working well so far.

The problem I'm having now is that Backbone doesn't know that at this point all models are synced with the server. So at a later point in the app, when I call model.save() on each model, it sends each model to the server again (which should be only the ones that are changed since the batch operation). How could I let Backbone know that all models are synced? I was looking at the 'changed' and 'hasChanged' attributes, but am not quite sure if I should manipulate these (I guess not).

like image 829
Bjorn Avatar asked Nov 29 '25 22:11

Bjorn


1 Answers

Backbone does not include the feature of tracking changed attributes since last sync with server.

The changed and hasChanged are not dealing with changed attributes since last sync with server.

You will have to create your own mechanism for tracking the state of your models:

  1. Each model should have a hasChangedSinceLastSync flag.
  2. Each model should bind (.on) to it's own change event and set the flag to true.
  3. Override the sync method and set the flag to false once data is returned from the server (for read/create/update).
like image 65
Tal Bereznitskey Avatar answered Dec 01 '25 11:12

Tal Bereznitskey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!