Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A wrapper around Backbone.sync to prevent model and collection request race conditions.
JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
.gitignore
.travis.yml
LICENSE
README.md
backbone-safesync.js

README.md

Backbone Safe Sync Build Status

A wrapper around Backbone.sync to prevent model and collection request race conditions.

Example

var bookSearch = new Books();

var xhr1 = bookSearch.fetch({ // imagine this request took 1000ms
    data: {
        q: 'javascript'
    }
});

var xhr2 = bookSearch.fetch({ // and this request took 50ms
    data: {
        q: 'dom'
    }
});

xhr1.state(); // 'rejected'
xhr2.state(); // 'pending'

Why?

Currently slow request can cause havok on the the state of your collections and models. Imagine this scenario: A user search for 'javascript' first and then 'dom'. The 'javascript' search hangs so the user never got to see the results. They then search for 'dom' and that response comes in right away while the 'javascript' search is still pending. The user is presented with books on 'dom'. The old 'javascript' request now responds. The collection and the users UI is abruptly changed to books on 'javascript'. Not good! backbone-safesync tried to remedy this by aborting old request.

Forcing Parallel Request (default behavior)

var bookSearch = new Books();

var xhr1 = bookSearch.fetch({
    data: {
        q: 'javascript'
    }
});

var xhr2 = bookSearch.fetch({
    safe: false,
    data: {
        q: 'dom'
    }
});

xhr1.state(); // 'pending'
xhr2.state(); // 'pending'
Something went wrong with that request. Please try again.