I have a Backbone Collection that users are performing CRUD-type activities on. I want to postpone any changes from being propagated back to the server — the Collection.sync()
should not happen until the user initiates it (like POSTING
a form).
As it stands, I have been able to implement on-the-fly updates with no issue (by calling things like Model.destroy()
on the models when deleted, or Collection.add()
to add new models to the collection. As I understand, I could pass the {silent:true}
option to my models, preventing .sync()
from being called during .add()
/.destroy()
, but from what I can tell, that could lead to some headaches later.
I have considered overriding Backbone.sync
, but I am not sure if that is the best route — I feel like there is some way to hook into some events, but I am not sure. Of course I have read through the Backbone docs, annotated source, and relevant SO questions before posting this, but I have hit a wall trying to extrapolate for this particular situation.
Eventually I will need to implement this in many places in my application, which is why I am concerned about best-practices at this stage. I am looking for some guidance/suggestions/thoughts on how to proceed with preventing the default behavior of immediately syncing changes with the remote server. Any help is appreciated — thank you for your time!
EDIT:
I went with Alex P's suggestion of refactoring: in my collection I set up some attributes to track the models that have been edited, added, or deleted. Then, when the user triggers the save action, I iterate through the lists and do the appropriate actions.