Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
An example of overriding Backbone.sync and Backbone.parse to load data from localStorage in Backbone
// Overriding Backbone.sync to either load locally or from the server
sync: function(method, model, options) {
// Pull the key and stored object localStorage
var storageKey = localStorage.getItem('storage');
var storageData = localStorage.getItem('storage-' + storageKey);
// Apply the callback with our local data
if(storageData) {
// No local data so run .getJSON to fetch it from the server
else {
$.getJSON(this.url, function(data, textStatus) {
if(textStatus !== 'success' || !data) {
// Overriding Backbone.parse to fill models from local data
parse: function(response) {
// Loading from localStorage so we intercept to properly
// serialize the object in our Backbone.collections
var self = this;
// In my case, the object loaded from storage was an array
// Backbone usually passes an object to parse
if(response.length) {
// Set properties on our model and return it
return {
myCollection: self.collection.add(response.myCollection),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.