extend Backbone.sync to batch sync?

What technique shall one use to implement batch insert/update for Backbone.sync?


I guess it depends on your usage scenarios, and how much you want to change the calling code. I think you have two options:

Option 1: No Change to client (calling) code

Oddly enough the annotated source for Backbone.sync gives 'batching' as a possible reason for overriding the sync method:

Use setTimeout to batch rapid-fire updates into a single request.

Instead of actually saving on sync, add the request to a queue, and only batch-save every so often. _.throttle or _.delay might help you here.

Option 2: Change client code

Alternatively, instead of calling save on your models, you could add some sort of save method to collections. You'd have to track which models were actually modified and hence in need of update, since as far as I can tell, Backbone only knows whether they're new or not (but I could be wrong about that).

Here's how I did it

Backbone.originalSync = Backbone.sync;

Backbone.sync = function (method, model, options) {
    // code to extend sync

    // calling original sync 
    Backbone.originalSync(method, model, options);

works fine for me , and I use it to control every ajax request coming out of any model or collection

Need Your Help

C# Universal Windows App Aggregate Exception (Raspberry Pi)

c# exception-handling raspberry-pi win-universal-app windows-10-iot-core

I'm developing an app that makes use of the FTDI drivers as described by this github project https://github.com/Jark/FTDISample