Skip to content

Instantly share code, notes, and snippets.

@cesarvarela
Created November 25, 2012 23:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesarvarela/4145876 to your computer and use it in GitHub Desktop.
Save cesarvarela/4145876 to your computer and use it in GitHub Desktop.
A VERY basic chrome.storage.local custom sync, method for Backbone.js Models
var createLocalStorageSync = function(storageKey)
{
return function(method, model, options)
{
switch (method)
{
case 'create':
case 'update':
chrome.storage.local.set({storageKey: JSON.stringify(model) }, options.success );
break;
case 'delete':
chrome.storage.local.remove(storageKey, options.success );
break;
case 'read':
chrome.storage.local.get(storageKey, function(items)
{
options.success( items[storageKey] && JSON.parse(items[storageKey]) || {} );
});
break;
}
return this;
};
};
// usage
SettingsModel = Backbone.Model.extend
({
// override sync with the custom sync method, and declare a key to be used when saving to localStorage
sync: createLocalStorageSync('settings'),
defaults:
{
'notifications-onmessage': true
}
});
This DOES NOT work on collections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment